This is a simple one. I have a folder ~/Dropbox/dotfiles/ and I want to create symlinks in my home folder to each file and folder in dotfiles. For example if dotfiles contains a file muttrc, without the leading dot, the scripts should create a link ~/.muttrc -> ~/Dropbox/dotfiles/muttrc. It should do this for each file and folder in dotfiles.
If a file or folder called ~/.muttrc (or whatever) already exists it should print out "File already exists at ~/.muttrc". If a symlink already exists it should print out "Already linked ~/.muttrc."
I made an attempt at this but it's not well written and doesn't seem to work quite right, it tries to link folders that already exist, and it seemed to overwrite a file that already existed once, and I think it has other bugs.
Thanks
Code:
#!/bin/bash
# This is a bash script that makes various symlinks in my home directory to
# files in my Dropbox folder.
# FIXME: code repetition of the if block! Should be some way to put it in a
# function and reuse it.
# FIXME: it seems that -f checks is a file exists, -a if a dir exists, and
# something else if a link exists, if _anything_ with a given name already
# exists then you can't make a link with that name, so the if function
# should check for the existence of all three.
# Link ~/bin to ~/Dropbox/bin. bin is where I put all my custom executable
# files and scripts.
if [ -h ~/bin ]
then
echo ~/bin: already linked
else
if [ -e ~/bin ]
then
echo ~/bin: file exists
else
echo linking ~/bin
ln -s ~/Dropbox/bin ~/bin
fi
fi
# Link to every dotfile in ~/Dropbox/dotfiles.
DIR=~/Dropbox/dotfiles
cd $DIR
for i in *; do
if [ -h ~/.$i ]
then
echo ~/.$i: already linked
else
if [ -f ~/.$i ]
then
echo ~/.$i: file already exists
else
echo linking .$i
ln -s $DIR/$i ~/.$i
fi
fi
done