![Problemas con script simple :(](https://rvso.com/image/1083746/Problemas%20con%20script%20simple%20%3A(.png)
¿Alguien podría decirme por qué este script no se ejecuta correctamente? Parece haber un problema con la declaración if ya que sigo recibiendo el 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'
La secuencia de comandos:
#!/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
Respuesta1
Como@Cyrus sugirió, probablemente tengas un carácter que no se imprime y que te está causando problemas. La razón más probable es que en algún momento editó este script desde una máquina con Windows y \r
se agregó un al final de la línea. Puedes comprobar si hay algo en esa línea ejecutando
grep 'if [ "$exit"' script.sh | od -c
Eso mostrará todos los caracteres, incluidos los que no se imprimen.
Si realmente tienes un problema \r
allí, puedes solucionarlo ejecutando "
sed -i 's/\r//' script.sh
O bien, instalando dos2unix
( sudo apt-get install dos2unix
) y ejecutando
dos2unix script.sh > fixed.sh