问题
I must be dense, but I simply cannot solve this. I have googled and searched here and nothing I find works.
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
I don't know shell scripting very well (actually, not well at all) but this really can't be rocket science!
I spent more than an hour banging my head against the wall to get the first command (cd) to work; it turned out that unless the quote character in "SkiAddict's" was a smart quote, 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.
Help???
回答1:
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.
回答2:
I found the answer last night but since I'm new here I couldn't reply to my own question then. Thanks to the Bash FAQ I was able to learn about $'...' 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'
Many thanks @askewchan for your help. Even though we didn't get anywhere together with it, it was still very nice to have someone to "talk" to about it :-)
来源:https://stackoverflow.com/questions/15048700/shell-script-and-quote-in-path