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
sinttx
  • 31
  • 1
  • 4

1 Answers1

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
terdon
  • 104,119