1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Bash shell script

Discussion in 'Mac Programming' started by SRossi, Jun 10, 2009.

  1. macrumors regular

    Right I am trying to create a backup shell script for college but i am getting an error and since I know little about shell scripts i thought i'd ask for a bit of help. The error i am getting is :

    I understand that means that there is something wrong with the ";;" at line 107 but i do not know what is wrong?

    Here is my code

    # Stephen Ross' Backup Script
    # declaring the variables and the path names
    Backup=/Users/rosstephen/Documents/Archive_$(date '+%d.%m.%y:%H:%M').tgz
    # begin while statement
    while [1] 
    # user choice menu
            echo "***************************************************"
            echo "                                   Backup and Restore         "
            echo "***************************************************"
            echo " Please select an option from 1-3:-"
            echo "1) Create a Backup"
            echo "2) Restore folders or files from a Backup"
            echo "3) Exit System"
    # Read user input
    # begin the case statement
    case $USERCHOICE in
    '1')	echo "Performing a Full Backup." 
    # pause for 2 seconds
            sleep 2
            echo "Please enter the complete pathname of the files that you would like to be backed up:-" 
    # read the user input and error correct path and on execution if it returns 0 t$
            read LOCATION
            if [ -d "$LOCATION" ]; then
            echo "you are going to backup the selected files in "$LOCATION" 
            echo "the files or folders will be backed up to the location" 
         echo "folder named mybackups" 
            sleep 3
            echo "That is an incorrect folder name, " 
            echo "please check that the directory is correct" 
            echo "then enter the folder or path name again remembering to be case sensitive." 
            read LOCATION
    # execute the backup
            tar -cf $Backup $LOCATION
    # check to see that the backup has completed correctly
            if [ $? == 0 ]; then
            echo "$Backup" has been successful."
            sleep 3
            echo "Your Backup could not be completed as a problem has occurred.."
            sleep 3
    '2')      echo "You are about to restore files or folders from a backup" 
             sleep 3
            echo "Listed below are any backups that are in the backup folder."
            ls "$Restore"
            echo "Please select the file you would like to restore"
            echo "Please enter the full pathname."
            read restorepath
            if [ -f "$restorepath" ]; then
            echo "the files will now be restored"
            sleep 4
            echo "An error has occurred with the filename entered."
            echo "Please re-enter the file name carefully"
            read restorepath
            Restore="$Restore" "$restorepath";
           echo "the files will now be restored"
            sleep 4
            tar -xf $Restore
            if [ $? == 0 ]; then
            echo "$restorepath" has been restored"
            sleep 4
            echo "There was an error during the restore"
    	echo "please try again or exit"
    '3')      echo "thank you, the program will now close "$USER" 
    *)      echo "sorry try again select 1, 2 or 3 only" 
    	echo "press enter to continue"
            read -n 1
    Thanks in advance :)

  2. macrumors 68040


    Over-quoting on the prior line:
    echo "thank you, the program will now close "$USER" 
    You have three "s. An odd number is not normally what you want.

  3. macrumors regular

    Thank you I have now took the quotes out of the file because I notice you dont need them I have also removed [1] from the while line at the top and now it is just running through the program.

    Any help :)

  4. macrumors 6502

    One quick thing to do is to run the script with debug output.
    bash -x script.sh
  5. macrumors regular

    Thanks I ran it like that but it still just runs through the whole script without allowing me to enter a number and when i do it just loops again.

    Im actually so lost lol

  6. macrumors 6502

    I noticed "Read" is capitalized when it shouldn't.
  7. macrumors regular

    Woooooooo :p

    Works now thanks man, see i would never have noticed that thanks :)

  8. macrumors regular

    Ok I now have a new problem after I have archived the file it will not un archive, any help?

    Thanks in advance again

  9. macrumors member

    What errors are you getting now??

    I'm not sure, but do you really want

    tar -xf $Restore
    in the restore section? I thought restorepath was the path to the tar file to untar. Do you want

    tar -xf $restorepath
    Also, just a side note, usually .tgz is a gzipped/tarred file, which you would use 'tar czf' and 'tar xzf' to tar/untar. I would either rename your file to just '.tar' or change your tar command to use the 'z' option. Since you are just using this internally it probably does not matter, but I think if you use 'tar cf' to create a tar file and someone uses 'tar xzf' to untar it, it won't work.
  10. macrumors P6


    just curious, but did you ever get this to work?
  11. macrumors regular

    Yeah I did, well not exactly working but enough for what was asked of me to do. :)


Share This Page