As many others have done, I want to create a repo to store my dotfile customizations. Instead of doing ln -s
manually, I am using the following script to set things up.
#!/bin/bash
set -e
DIR="$HOME/Documents/Dotfiles"
OLDDIR="$HOME/Documents/Other\ Files/Dotfiles_old"
FILES=($HOME/.bash_profile)
echo "Creating $OLDDIR for backup of any existing dotfiles in ~"
mkdir -p "$OLDDIR"
echo "…done"
echo "Changing to the $DIR directory"
cd "$DIR"
echo "…done"
for FILE in "${FILES[@]}"; do
echo "Backup dotfile $FILE from ~/ to $OLDDIR"
cp -L "$HOME/$FILE" "$OLDDIR"
done
for FILE in "${FILES[@]}"; do
echo "copy $FILE from ~ to $DIR."
cp -L "$HOME/$FILE $DIR/"
echo "Creating symlink to $FILE from ~ to $DIR."
ln -sfn "$DIR/$FILE" "$HOME/$FILE";
done
shellcheck source "$HOME/.bash_profile"
When I run this, cp
fails because it thinks that .bash_profile
isn't there, which obviously isn't the case:
I think my path to the files may be incorrect, although shellcheck
reports nothing. What am I forgetting here?
UPDATE: Made another run at this - minus the cp
. The one thing I am still unsure of is the use of exit
, in particular since I'm already using -e
to check for errors.
Shellcheck and bash -n
return 0.
#!/bin/bash
set -e
function makeFiles() {
touch .bash_profile \
touch .gitconfig \
touch .gitignore_global
}
function makeLinks() {
ln -sfn ~/Documents/Dotfiles/.bash_profile ~/.bash_profile \
ln -sfn ~/Documents/Dotfiles/.gitconfig ~/.gitconfig \
ln -sfn ~/Documents/Dotfiles/.gitignore_global ~/.gitignore_global \
source ~/.bash_profile
}
read -rp "This may overwrite existing files. Are you sure? (y/n) " -n 1;
echo "";
if [[ $REPLY =~ ^[Yy]$ ]]; then
makeFiles && makeLinks
fi;
Sigh, ln
decides that .bash_profile
needs to be a directory for some crazy reason.