Bash

我的 dotfiles bash 腳本以某種方式刪除了自己的目錄?

  • July 21, 2020

我使用 git 在多台機器上跟踪我的點文件。我在 repo 中編寫了一個非常簡單的腳本,它備份任何過時的現有 dotfiles,然後創建指向每個最新 dotfiles 的符號連結。這是腳本:

#!/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 遠端),當我第一次執行這個腳本時,**它刪除了它所在的目錄。**我不知道它是怎麼做到的我編寫的腳本,並且在我第二次執行它時它正常工作(再次重新複製儲存庫之後)。出了什麼問題,我該如何解決?這是git的錯嗎?這是我的 bash 終端在 bug 周圍的樣子(為了清楚起見,我在此處添加了一些帶有 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:~$ 

這是您的問題中帶註釋的輸出:

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的 HOME 目錄中沒有包含預期文件的舊目錄),例如:

[[ -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"; }

您可以根據需要添加其他類似的防禦檢查。

引用自:https://unix.stackexchange.com/questions/599357