Need help with simple script...

Discussion in 'Mac Programming' started by kellah, Feb 9, 2008.

  1. macrumors regular

    Joined:
    Apr 29, 2007
    Location:
    East Lansing, MI
    #1
    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:

    Code:
    #!/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?
     
  2. macrumors regular

    Joined:
    Jul 23, 2002
    Location:
    OR
    #2
    I believe you want something like this:

    Code:
    #!/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. :)
     
  3. macrumors 68000

    aross99

    Joined:
    Dec 17, 2006
    Location:
    East Lansing, MI
    #3
    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...
     
  4. thread starter macrumors regular

    Joined:
    Apr 29, 2007
    Location:
    East Lansing, MI
    #4
    Thanks for the responses; with your help I got it working now!
     

Share This Page