내 dotfiles bash 스크립트가 어떻게든 자신의 디렉토리를 삭제합니까?

내 dotfiles bash 스크립트가 어떻게든 자신의 디렉토리를 삭제합니까?

저는 git을 사용하여 여러 컴퓨터에서 도트 파일을 추적합니다. 나는 오래된 기존 도트 파일을 백업한 다음 각 최신 도트 파일에 대한 심볼릭 링크를 생성하는 매우 간단한 스크립트를 저장소에 작성했습니다. 스크립트는 다음과 같습니다.

#!/bin/bash
############################
# makesymlinks.sh
# This script creates symlinks from the home directory to any desired dotfiles in ~/dotfiles
############################

########## Variables

dir=~/dotfiles              # dotfiles directory
olddir=~/dotfiles_old           # old dotfiles backup directory
files="bash_aliases bashrc vimrc"   # list of files/folders to symlink in homedir

##########

# create dotfiles_old in homedir
echo "Creating $olddir for backup of any existing dotfiles in ~"
mkdir -p $olddir

# move any existing dotfiles in homedir to dotfiles_old directory, then create symlinks 
echo "Moving any existing dotfiles from ~ to $olddir"
for file in $files; do
    if [ -f ~/."$file" ]; then
        mv -n ~/."$file" ~/dotfiles_old/    #-n option means don't overwrite existing files in dotfiles_old
    fi

    #if e.g. ~/.vimrc exists after mv command, then this script must've been run before w/ .vimrc included
    if [ -f ~/."$file" ]; then
        echo "Symlink to $dir/$file already exists"
    else
        echo "Creating symlink to $dir/$file in ~"
        ln -s $dir/"$file" ~/."$file"
    fi
done

# source .bashrc
printf "\nTo complete the setup, please run the following command:\n\n"
printf "\tsource ~/.bashrc\n\n"

이 스크립트는 일반적으로 매우 잘 작동합니다. 오늘 제가 새 컴퓨터에서 작업을 시작하는 동안(중요하다면 원격으로 TeamViewer를 통해) 이 스크립트를 처음 실행했을 때,해당 디렉토리가 있던 디렉토리를 삭제합니다.내가 작성한 스크립트를 기반으로 이 작업이 어떻게 수행되는지 모르겠지만 두 번째로 실행했을 때 제대로 작동했습니다(저장소를 다시 복제한 후). 무엇이 잘못되었으며 어떻게 해결합니까? 이게 자식 잘못인가요? 오류 주변의 bash 터미널은 다음과 같습니다(명확성을 위해 여기에 몇 가지 bash 주석을 추가했습니다).

drakeprovost@shatterdome:~/RoverCoreOS$ git clone https://github.com/DrakeProvost/dotfiles.git
Cloning into 'dotfiles'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 42 (delta 21), reused 29 (delta 11), pack-reused 0
Unpacking objects: 100% (42/42), done.
drakeprovost@shatterdome:~/RoverCoreOS$ cd dotfiles/
drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ls
bash_aliases  bashrc  makesymlinks.sh  README.md  vimrc
drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ./makesymlinks.sh 
Creating /home/drakeprovost/dotfiles_old for backup of any existing dotfiles in ~
Moving any existing dotfiles from ~ to /home/drakeprovost/dotfiles_old
Creating symlink to /home/drakeprovost/dotfiles/bash_aliases in ~
Creating symlink to /home/drakeprovost/dotfiles/bashrc in ~
Creating symlink to /home/drakeprovost/dotfiles/vimrc in ~

To complete the setup, please run the following command:

    source ~/.bashrc

drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ls
bash_aliases  bashrc  makesymlinks.sh  README.md  vimrc
drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ cd
drakeprovost@shatterdome:~$ ls -al #.bashrc, .vimrc, and .bash_aliases were all red symlinks here, meaning they pointed to non-existent files. Also note that the dotfiles directory has disappeared
total 144
drwxr-xr-x 26 drakeprovost drakeprovost  4096 Jul 19 22:40 .
drwxr-xr-x 12 root         root          4096 Sep 24  2019 ..
lrwxrwxrwx  1 drakeprovost drakeprovost    40 Jul 19 22:40 .bash_aliases -> /home/drakeprovost/dotfiles/bash_aliases
-rw-------  1 drakeprovost drakeprovost 11400 Feb 27 20:01 .bash_history
-rw-r--r--  1 drakeprovost drakeprovost   220 Sep 17  2019 .bash_logout
lrwxrwxrwx  1 drakeprovost drakeprovost    34 Jul 19 22:40 .bashrc -> /home/drakeprovost/dotfiles/bashrc
drwx------ 15 drakeprovost drakeprovost  4096 Oct 15  2019 .cache
drwxr-xr-x  5 drakeprovost drakeprovost  4096 Feb 20 18:08 catkin_ws
drwxr-xr-x  5 drakeprovost drakeprovost  4096 Feb 27 19:23 catkin_ws_PMCurdf
drwx------ 13 drakeprovost drakeprovost  4096 Feb 27 18:57 .config
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Desktop
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Documents
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Jul 19 22:40 dotfiles_old
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Downloads
-rw-r--r--  1 drakeprovost drakeprovost  8980 Sep 17  2019 examples.desktop
drwx------  2 drakeprovost drakeprovost  4096 Oct 15  2019 .gconf
drwx------  3 drakeprovost drakeprovost  4096 Oct 15  2019 .gnupg
-rw-------  1 drakeprovost drakeprovost  2052 Jul 19 22:31 .ICEauthority
drwx------  3 drakeprovost drakeprovost  4096 Oct 15  2019 .local
drwx------  5 drakeprovost drakeprovost  4096 Oct 15  2019 .mozilla
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Music
drwx------  6 drakeprovost drakeprovost  4096 Jul 19 22:31 .nx
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Pictures
-rw-r--r--  1 drakeprovost drakeprovost   807 Sep 17  2019 .profile
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Public
drwx------  2 drakeprovost drakeprovost  4096 Jul 19 22:31 .qt
drwxr-xr-x  4 drakeprovost drakeprovost  4096 Feb 27 19:58 .ros
drwxr-xr-x 11 drakeprovost drakeprovost  4096 Jul 19 22:40 RoverCoreOS
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Feb 13 13:45 .rviz
drwxr-xr-x  3 drakeprovost drakeprovost  4096 Oct 15  2019 snap
drwx------  2 drakeprovost drakeprovost  4096 Oct 15  2019 .ssh
-rw-r--r--  1 drakeprovost drakeprovost     0 Oct 15  2019 .sudo_as_admin_successful
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Templates
drwxr-xr-x  2 drakeprovost drakeprovost  4096 Oct 15  2019 Videos
-rw-------  1 drakeprovost drakeprovost   761 Oct 15  2019 .viminfo
lrwxrwxrwx  1 drakeprovost drakeprovost    33 Jul 19 22:40 .vimrc -> /home/drakeprovost/dotfiles/vimrc
drakeprovost@shatterdome:~$ source ~/.bashrc
bash: /home/drakeprovost/.bashrc: No such file or directory
drakeprovost@shatterdome:~$ git clone https://github.com/DrakeProvost/dotfiles.git
Cloning into 'dotfiles'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 42 (delta 21), reused 29 (delta 11), pack-reused 0
Unpacking objects: 100% (42/42), done.
drakeprovost@shatterdome:~$ cd dotfiles
drakeprovost@shatterdome:~/dotfiles$ ./makesymlinks.sh 
Creating /home/drakeprovost/dotfiles_old for backup of any existing dotfiles in ~
Moving any existing dotfiles from ~ to /home/drakeprovost/dotfiles_old
Creating symlink to /home/drakeprovost/dotfiles/bash_aliases in ~
Symlink to /home/drakeprovost/dotfiles/bashrc already exists
Creating symlink to /home/drakeprovost/dotfiles/vimrc in ~

To complete the setup, please run the following command:

    source ~/.bashrc

drakeprovost@shatterdome:~/dotfiles$ cd
drakeprovost@shatterdome:~$ ls #notice that dotfiles still exists this time
catkin_ws          Documents     Downloads         Pictures     snap
catkin_ws_PMCurdf  dotfiles      examples.desktop  Public       Templates
Desktop            dotfiles_old  Music             RoverCoreOS  Videos
drakeprovost@shatterdome:~$ source ~/.bashrc #this now works like you would expect
drakeprovost@shatterdome:~$ 

답변1

귀하의 질문에 대한 의견의 결과는 다음과 같습니다.

drakeprovost@shatterdome:~/RoverCoreOS$ git clone https://github.com/DrakeProvost/dotfiles.git
Cloning into 'dotfiles'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 42 (delta 21), reused 29 (delta 11), pack-reused 0
Unpacking objects: 100% (42/42), done.

~/RoverCoreOS참고: 위 명령을 실행하면 디렉터리에 있으므로 git clone위 명령은 디렉터리 ~/RoverCoreOS/dotfiles가 아닌 디렉터리를 생성합니다 ~/dotfiles.

drakeprovost@shatterdome:~/RoverCoreOS$ cd dotfiles/
drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ls
bash_aliases  bashrc  makesymlinks.sh  README.md  vimrc
drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ./makesymlinks.sh 
Creating /home/drakeprovost/dotfiles_old for backup of any existing dotfiles in ~
Moving any existing dotfiles from ~ to /home/drakeprovost/dotfiles_old
Creating symlink to /home/drakeprovost/dotfiles/bash_aliases in ~
Creating symlink to /home/drakeprovost/dotfiles/bashrc in ~
Creating symlink to /home/drakeprovost/dotfiles/vimrc in ~

To complete the setup, please run the following command:

    source ~/.bashrc

drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ ls
bash_aliases  bashrc  makesymlinks.sh  README.md  vimrc

위의 모든 일은 에서 일어났습니다 ~/RoverCoreOS/dotfiles.

drakeprovost@shatterdome:~/RoverCoreOS/dotfiles$ cd

당신은 이제 디렉토리에 있습니다~

drakeprovost@shatterdome:~$ ls -al #.bashrc, .vimrc, and .bash_aliases were all red symlinks here, meaning they pointed to non-existent files. Also note that the dotfiles directory has disappeared

~/dotfiles사라진 것이 아니고 존재한 적도 없습니다. ~/RoverCoreOS/dotfiles존재했고 지금도 존재할 수 있다.

...
drakeprovost@shatterdome:~$ git clone https://github.com/DrakeProvost/dotfiles.git
Cloning into 'dotfiles'...
remote: Enumerating objects: 42, done.
remote: Counting objects: 100% (42/42), done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 42 (delta 21), reused 29 (delta 11), pack-reused 0
Unpacking objects: 100% (42/42), done.

지금디렉터리를 만들었고 ~/dotfiles이제부터는 모든 것이 예상대로 작동합니다.

몇 가지 방어 검사를 추가하려면 스크립트를 수정하는 것이 좋습니다. 그들은 당신이 위의 작업을 수행하는 것을 막을 수는 없지만 최소한 몇 가지 문제에 대해 경고할 수 있으며 위의 문제를 포착할 것입니다( dotfiles예상 파일이 포함된 홈 디렉토리에 이전 디렉토리가 없다고 가정 ). 예를 들어:

[[ -d "$dir" ]] || { ret="$?"; echo "dir \"$dir\" does not exist"; exit "$ret"; }

for file in $files; do
    [[ -s "$dir/$file" ]] || { ret="$?"; echo "file \"$dir/$file\" does not exist or is empty"; exit "$ret"; }
done

# create dotfiles_old in homedir
echo "Creating $olddir for backup of any existing dotfiles in ~"
mkdir -p "$olddir" || { ret="$?"; echo "Failed to create olddir \"$olddir\""; exit "$ret"; }

필요에 따라 다른 유사한 방어 검사를 추가할 수 있습니다.

관련 정보