Bash shell script

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

  1. macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #1
    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

    Code:
    #!/bin/bash
    # Stephen Ross' Backup Script
    
    # declaring the variables and the path names
    Backup=/Users/rosstephen/Documents/Archive_$(date '+%d.%m.%y:%H:%M').tgz
    
    Restore=/Users/rosstephen/Documents
    
    # begin while statement
    while [1] 
    do
    
    # user choice menu
    
            clear
            echo "***************************************************"
            echo "                                   Backup and Restore         "
            echo "***************************************************"
            echo " Please select an option from 1-3:-"
            echo 
            echo "1) Create a Backup"
            echo 
            echo "2) Restore folders or files from a Backup"
            echo 
            echo "3) Exit System"
            echo 
    
    # Read user input
    Read USERCHOICE
    
    # 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
            else
            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
    
            fi
    # 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
    
            else
    
            echo "Your Backup could not be completed as a problem has occurred.."
            sleep 3
            fi
            ;;
    
    '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
           else
    
            echo "An error has occurred with the filename entered."
            echo "Please re-enter the file name carefully"
            read restorepath
            Restore="$Restore" "$restorepath";
    
            Fi
           echo "the files will now be restored"
            sleep 4
            tar -xf $Restore
            if [ $? == 0 ]; then
            echo "$restorepath" has been restored"
            sleep 4
    
            else
    
            echo "There was an error during the restore"
    	echo "please try again or exit"
    
            fi
            ;;
    
    '3')      echo "thank you, the program will now close "$USER" 
            exit
            ;;
    
    *)      echo "sorry try again select 1, 2 or 3 only" 
    	echo "press enter to continue"
            read -n 1
            ;;
            esac
    
    done
    
    
    Thanks in advance :)

    Stephen
     
  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Over-quoting on the prior line:
    Code:
    echo "thank you, the program will now close "$USER" 
    You have three "s. An odd number is not normally what you want.

    -Lee
     
  3. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #3
    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 :)

    Stephen
     
  4. macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #4
    One quick thing to do is to run the script with debug output.
    bash -x script.sh
     
  5. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #5
    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

    Stephen
     
  6. macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #6
    I noticed "Read" is capitalized when it shouldn't.
     
  7. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #7
    Woooooooo :p

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

    Stephen
     
  8. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #8
    Ok I now have a new problem after I have archived the file it will not un archive, any help?

    Thanks in advance again

    Stephen
     
  9. macrumors member

    Joined:
    Aug 26, 2008
    #9
    What errors are you getting now??

    I'm not sure, but do you really want

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

    Code:
    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

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #10
    just curious, but did you ever get this to work?
     
  11. thread starter macrumors regular

    Joined:
    May 27, 2009
    Location:
    Glasgow, Scotland
    #11
    Yeah I did, well not exactly working but enough for what was asked of me to do. :)

    Stephen
     

Share This Page