I have adapted a shell script to symlink my dotfiles from a git repository into my home directory. It works well except for one issue... it seems to be creating a symlink of a directory inside the directory itself.
In the below script, the .vscode
entry in the files variable is a directory. When I run the script initially (when no symlinks are created) everything works fine. When I run the script a second time it still works fine. However, on the third run, a .vscode symlink is created WITHIN the ~/projects/dotfiles/.vscode folder.
#!/bin/bash
############################
# makesymlinks.sh
# This script creates symlinks from the home directory to any desired dotfiles in ~/projects/dotfiles
############################
########## Variables
dir=~/projects/dotfiles # dotfiles directory
olddir=~/dotfiles_old # old dotfiles backup directory
files=(.vscode .bash_profile .zshrc .gitconfig) # list of files/folders to symlink in homedir
##########
# create dotfiles_old in homedir
echo -n "Creating $olddir for backup of any existing dotfiles in ~ ..."
mkdir -p $olddir
echo "done"
# change to the dotfiles directory
echo -n "Changing to the $dir directory ..."
cd $dir
echo "done"
# move any existing dotfiles in homedir to dotfiles_old directory, then create symlinks from the homedir to any files in the ~/projects/dotfiles directory specified in $files
for file in $files; do
echo "Moving any existing dotfiles from ~ to $olddir"
mv ~/$file $olddir/
echo "Creating symlink to $file in home directory."
ln -s $dir/$file ~/$file
done
I would like to prevent this from happening, but I cannot see why it is. How can I prevent this?