skiaddict1
skiaddict1

Reputation: 525

Shell script and quote in path

I have a simple shell script. Currently it looks as below. The diff command fails with "No such file or directory" on the second argument (the one in red below).

#!/bin/bash
    
set -x

cd "/Volumes/$1/Backups.backupdb/SkiAddict’s Mac/Latest"
time diff -qr /Users/n/Documents/Code "SkiAddict’s Mac/Users/n/Documents/Code"

I call the script like this from the Terminal:

bash myScript.sh Quadra

To get the first command (cd) to work, it turned out that unless the quote character in "SkiAddict's" was a smart quote, and I got the same error (No such file or directory). Now that line is working fine and even if I copy the text from that line and paste it into the diff command, it doesn't work.

This entire script was copied from an already-working QuicKeys macro which types the text into a Terminal window, so I really figured making a shell script wouldn't be a biggie. The macro begins by "typing" this line into a new Terminal tab:

cd /Volumes/Quadra/Backups.backupdb/SkiAddict’s\ Mac/Latest

It then presses Enter and "types" this line:

time diff -qr /Users/n/Documents/Code "SkiAddict's Mac/Users/n/Documents/Code"

Again, it then presses Enter. The thing works perfectly.

I've tried double quotes at either end, single quotes at either end, escaping the quote, both with and without double or single quotes at either end. Nothing works.

What should I do?

Upvotes: 2

Views: 3343

Answers (2)

skiaddict1
skiaddict1

Reputation: 525

Thanks to the Bash FAQ I was able to learn about $'...' (thus $'SkiAddict\'s below). This enables the single quote in the path to be backslashed, which makes the diff command happy.

And no, the cd command cannot be treated in the same way. If I change anything about that line, it doesn't work again!!! Sigh...

So, the two lines look like this:

cd "/Volumes/$1/Backups.backupdb/SkiAddict’s Mac/Latest"
time diff -qr /Users/n/Documents/Code $'SkiAddict\'s Mac/Users/n/Documents/Code'

Upvotes: 1

askewchan
askewchan

Reputation: 46530

To see what your script is actually doing, put some echo commands in your script to see if it's expanding the quotes and escapes as you expect. Just replace the time diff with echo and if it prints out the paths differently from how you would want them to be run, modify them accordingly.


I think actually the second file you're trying to open probably doesn't exist. It's most likely that that the path you want is:

/Users/n/Documents/Code

On Unix (or Mac) machine, all paths start in the 'root' directory, also know simply as:

/

Not the 'name' of your computer, which for you is SkiAddict's Mac


On the other hand, if you're actually trying to access a directory in the TimeMachine volume, you need to have a relative path, which might just simply be incorrect in your script. Try using the full path, starting with /Volumes/... in the diff line. As it stands, after the cd this is the file it will try to compare to:

"/Volumes/$1/Backups.backupdb/SkiAddict’s Mac/Latest/SkiAddict’s Mac/Users/n/Documents/Code"

You can determine what the correct path should be by typing cd /Volu... into your terminal and using tab-completion until you are on the TimeMachine. The tab-completion will properly escape spaces and special characters and will find an existing path. Perhaps this will help you find the error in your pathname.

Upvotes: 1

Related Questions