2

Could someone please tell me why this script doesn't run correctly. Seems to be a problem with the if statement as i keep getting the error:

/home/sinttx/Development/backup/ddbackup.sh: line 18: syntax error near unexpected token `then'
/home/sinttx/Development/backup/ddbackup.sh: line 18:  if [ "$exit" -eq 0 ] ; then'

The script:

#!/bin/bash

#(SINTTX) Function and if statement to backup/clone /dev/???.

 echo "
 THIS WILL COMPLETE A BACKUP OF DESIRED /DEV "

 echo -n "Enter desired name for saved file (File will be saved with extention .iso) and press [ENTER]: "
 read iso  
 echo -n "Enter the device to be duplicated/backed up (Eg /dev/sda) and press [ENTER]: "  
 read dev  
 echo -n "Enter the desired path/location for the backup file (Eg /home/USERNAME/backups/) and press [ENTER]: "  
 read path  
 echo "PLEASE ENTER THE SUDO PASSWORD (IF PROMPTED) AND WAIT :-)  
 NOTE: THIS COULD TAKE SOME TIME DEPENDING ON FILE SIZE AND HARDWARE SPEED!!"  
 sudo dd if="$dev" of="$path"/"$iso"  
 exit=$?  
 if [ "$exit" -eq 0 ] ; then  
  echo "SUCCESS"  
 else  
  echo "BACKUP HAS SUFFERED AN ERROR AND DID NOT COMPLETE SUCCESSFULLY"  
fi
7
  • 1
    @sinttx: Have you modified the script on a MS Windows system?
    – Cyrus
    Oct 11, 2014 at 9:57
  • hi @Jan thanks i tried your advice but still the same problem :(
    – sinttx
    Oct 11, 2014 at 9:59
  • @Cyrus Nope I am using Kubuntu only
    – sinttx
    Oct 11, 2014 at 10:00
  • @sinttx: feel free to check your file for non printable characters: cat -v ddbackup.sh.
    – Cyrus
    Oct 11, 2014 at 10:07
  • 1
    THANKS @Cyrus I was incorrect there was one non printable character! I will need to remember this command. Thanks again works a treat :)
    – sinttx
    Oct 11, 2014 at 10:20

1 Answer 1

1

As @Cyrus suggested, you probably have a non-printing character there that is causing you problems. The most likely reason is that at some point you edited this script from a Windows machine and a \r was added to the end of the line. You can check if anything is there on that line by running

grep 'if [ "$exit"' script.sh | od -c

That will show all characters, including non-printing ones.

If you do indeed have a \r there, you can fix it by running"

sed -i 's/\r//' script.sh

Or, installing dos2unix (sudo apt-get install dos2unix) and running

dos2unix script.sh > fixed.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.