PDA

View Full Version : Need help with simple script...




kellah
Feb 9, 2008, 02:58 PM
I found a bunch of posts on the internet about this but I'm still running into problems. Here's my goal of my script: check to see if my external hard drive is mounted. (It's called "Files") and then use ditto to copy all the files over. I've got ditto to work but now that I'm trying to do an if-else statement it has problems.

Here's what I've got:

#!/bin/sh

if (-e $d/$f) then
ditto -V /Users/andrew/Misc /Volumes/Files/Backup
else
echo "External drive not mounted!"
endif


When I try to run the script I get this error:

./backup: line 9: syntax error: unexpected end of file

What am I doing wrong?



numero
Feb 9, 2008, 03:51 PM
I believe you want something like this:

#!/bin/sh

if [ -e $d/$f ]
then
ditto -V "/Users/andrew/Misc /Volumes/Files/Backup"
else
echo "External drive not mounted!"
fi

You will need the quotes around the path name if there is a space after "Misc". You won't if there isn't.

In the "if" statement, the spaces around the square brackets are very important. They don't like anything touching them. Very "separatist" of them. :)

aross99
Feb 9, 2008, 09:04 PM
The syntax you are using is from the C-shell (csh) or Korn shell (ksh), not the Bourne shell (sh) that you have specified on the first line.

If you specify "sh" on the 1st line, you need to use the square brackets, etc as the poster above has specified. Try changing line 1 to specify csh or ksh if you want to use your original syntax...

kellah
Feb 11, 2008, 09:25 AM
Thanks for the responses; with your help I got it working now!