11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Capitolo 29. Debugging<br />

Debugging is twice as hard as writing<br />

the code in the first place. Therefore, if<br />

you write the code as cleverly as<br />

possible, you are, by definition, not<br />

smart enough to debug it.<br />

Brian Kernighan<br />

La shell <strong>Bash</strong> non possiede alcun debugger e neanche coman<strong>di</strong> o costrutti specifici per il debugging. 1 Gli<br />

errori <strong>di</strong> sintassi o le errate <strong>di</strong>gitazioni generano messaggi d’errore criptici che, spesso, non sono <strong>di</strong> alcun<br />

aiuto per correggere uno script che non funziona.<br />

Esempio 29-1. Uno script errato<br />

#!/bin/bash<br />

# ex74.sh<br />

# Questo è uno script errato.<br />

# Ma dove sarà mai l’errore?<br />

a=37<br />

if [$a -gt 27 ]<br />

then<br />

echo $a<br />

fi<br />

exit 0<br />

Output dell’esecuzione dello script:<br />

./ex74.sh: [37: command not found<br />

Cosa c’è <strong>di</strong> sbagliato nello script precedente (suggerimento: dopo if)?<br />

Esempio 29-2. Parola chiave mancante<br />

#!/bin/bash<br />

# missing-keyword.sh: Che messaggio d’errore verrà generato?<br />

for a in 1 2 3<br />

do<br />

echo "$a"<br />

# done # La necessaria parola chiave ’done’, alla riga 7,<br />

#+ è stata commentata.<br />

exit 0<br />

515

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!