PDA

View Full Version : [bash] script di shell


gepeppe
09-03-2008, 15:20
Salve, ho fatto un piccolo script di shell, che ogni volta che viene avviato, mi fa un backup di una cartella, quindi uso i comandi cp, rm, mkdir. Ecco un esempio:

#!/bin/sh

#Elimino il contenuto della cartella
rm -r /home/peppe/backup

#risalvo il tutto
mkdir /home/peppe/backup
cp -r /home/peppe/A/* /home/peppe/backup

Ora come faccio a far uscire un messaggio di errore se uno dei 3 comandi non ha dato esito positivo (es se la cartella nn esiste, o la copia nn č andata abuon fine)???

grazie

thehuge
09-03-2008, 18:28
Puoi usare la variabile $? che contiene in ogni momento il codice di errore dell'ultimo comando eseguito sulla shell corrente (se il comando č andato a buon fine esso contiene il valore 0).

Nel tuo esempio io farei cosė:
#!/bin/sh
#Elimino il contenuto della cartella
rm -r /home/peppe/backup
err=$?
if ! [[ $err -eq 0 ]]; then
echo "ERRORE $err" 1>&2
return $err
fi

#risalvo il tutto
mkdir /home/peppe/backup
err=$?
if ! [[ $err -eq 0 ]]; then
echo "ERRORE $err" 1>&2
return $err
fi

cp -r /home/peppe/A/* /home/peppe/backup
err=$?
if ! [[ $err -eq 0 ]]; then
echo "ERRORE $err" 1>&2
return $err
fi

Hardware Upgrade Forum Database Error
Database Error Database error
The Hardware Upgrade Forum database has encountered a problem.

Please try the following:
  • Load the page again by clicking the Refresh button in your web browser.
  • Open the www.hwupgrade.it home page, then try to open another page.
  • Click the Back button to try another link.
The www.hwupgrade.it forum technical staff have been notified of the error, though you may contact them if the problem persists.
 
We apologise for any inconvenience.