0

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:

enter image description here

enter image description here

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.

Chris
  • 547
  • 6
  • 20
  • 2
    Why do you think you need all those trailing backslashes??? remove them, it seems you have no idea what they should be used for. As a side note: why are you unsetting the variables `makeFiles`, `unset` and `makeLinks`? another side note: why do you use so many trailing semi-colons? – gniourf_gniourf Sep 21 '16 at 20:27

1 Answers1

0

You're building the path of the dotfile incorrectly - $FILE already contains the full path of the dotfile, and there's no need to prepend $HOME again. Try with this cp command:

cp -L "$FILE $DIR/"
John Bupit
  • 10,406
  • 8
  • 39
  • 75