Problemas com script simples :(

Problemas com script simples :(

Alguém poderia me dizer por que esse script não funciona corretamente. Parece haver um problema com a instrução if, pois continuo recebendo o erro:

/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'

O roteiro:

#!/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

Responder1

Como@Cyrus sugeriu, você provavelmente tem um caractere não imprimível que está causando problemas. O motivo mais provável é que em algum momento você editou esse script em uma máquina Windows e um \rfoi adicionado ao final da linha. Você pode verificar se há alguma coisa nessa linha executando

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

Isso mostrará todos os caracteres, inclusive os não imprimíveis.

Se você realmente tiver um \rproblema, poderá corrigi-lo executando"

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

Ou, instalando dos2unix( sudo apt-get install dos2unix) e executando

dos2unix script.sh > fixed.sh

informação relacionada