-1

I'm trying to write a dotfiles installer/initializer script that works in the newer MacOS zsh and I'm having trouble. Here's my script:

#!/bin/sh

BACKUP_DIR="~/.backup"
echo "Checking $BACKUP_DIR directory (This check is broken on zsh but should work on bash)"
while [ -d "$BACKUP_DIR" ]
do
  BACKUP_DIR="$BACKUP_DIR_$RANDOM"
  echo "the backup folder already exists trying $BACKUP_DIR instead"
done

echo "backing up existing dot files."
for each in `ls -lad \.* | grep ^- | rev | cut -w -f 1 | rev`
do
  mv ~/$each $BACKUP_DIR/$each
done

here's what I get when I run it:

cliftoncraig@Cliftons-MBP .dotfiles % ./install 
Checking ~/.backup directory (This check is broken on zsh but should work on bash)
backing up existing dot files.
mv: rename /Users/cliftoncraig/.gitconfig to ~/.backup/.gitconfig: No such file or directory
mv: rename /Users/cliftoncraig/.zprofile to ~/.backup/.zprofile: No such file or directory
cliftoncraig@Cliftons-MBP .dotfiles % ls ~/.gitconfig 
/Users/cliftoncraig/.gitconfig

As you can see, when I validate that the files it's trying to backup actually exist it leaves me puzzled. What am I doing wrong?

Cliff
  • 10,586
  • 7
  • 61
  • 102
  • Tilde doesn't expand to the home directory when it's in quotes. So either use `BACKUP_DIR=~/.backup` or `BACKUP_DIR="$HOME/.backup"`. See ["Getting negative on test for HOME directories with tilde ~ expansion"](https://stackoverflow.com/questions/25418238/getting-negative-on-test-for-home-directories-with-tilde-expansion/25523755#25523755). I'd also recommend running your code through [shellcheck.net](https://www.shellcheck.net] (it doesn't do zsh, but that's just the default *interactive* shell on macOS -- the default shell for scripts is still bash). – Gordon Davisson Aug 29 '23 at 03:47
  • Why are you tagging this _zsh_? There is no zsh involved in your question. – user1934428 Aug 29 '23 at 09:23

0 Answers0