Lazy Bash Auto Install

Sometimes you have a dotfile repository and need an auto installer.

I always like to execute bash with bash -x for maximum debugging goodness

My dotfiles repo consists of both regular files and directories. I’m going to loop through both and run a subroutine on them, for ultimate complexity.

Artifically constructing a for loop here is easy, and it’s going be be simpler to just manually keep track of files. So lets say you have profile, screenrc, and vimrc,

autoinstall.sh
1
2
3
4
for f in profile screenrc vimrc
  do
    subfile
  done

Don’t forget you have to declare your subroutines/functions before they are executed

And now the sub_file can just unlink some each file (note it still has access to the $f variable)

autoinstall.sh
1
2
3
4
subfile () {
  [ -f "$HOME/.$f" ] && unlink "$HOME/.$f"
  ln -s "$PWD/$f" "$HOME/.$f"
}

Taking advantage of the built-in $HOME and $PWD I believe is a good practice.

Ok, cool, you’re done. For directories, use the -d flag to check if the directory exists or not.