0

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?

Lee Colarelli
  • 151
  • 2
  • 13
  • 1
    What shell are you actually using? Your `for` loop should only ever execute once, as `$files` is the same as `${files[0]}`. Use `for file in "${files[@]}"; do` instead. – chepner Jul 24 '19 at 14:44
  • @chepner I'm in the zsh shell but i thought the makesymlinks.sh would still be run as bash? I tried your suggestion but the same thing still happens :/ – Lee Colarelli Jul 25 '19 at 15:00

1 Answers1

0

From the man page of ln(1):

-n    If the target_file or target_dir is a symbolic link, do not follow
      it.  This is most useful with the -f option, to replace a symlink
      which may point to a directory.

Try updating your ln -s command to use ln -sfn instead.

James Tomasino
  • 3,520
  • 1
  • 20
  • 38