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.

fi<br />

Capitolo 7. Verifiche<br />

Il costrutto if test con<strong>di</strong>zione-vera è l’esatto equivalente <strong>di</strong> if [ con<strong>di</strong>zione-vera ].<br />

In quest’ultimo costrutto, la parentesi quadra sinistra [, è un simbolo che invoca il comando test. La<br />

parentesi quadra destra <strong>di</strong> chiusura, ], non dovrebbe essere necessaria. Ciò nonostante, le più recenti<br />

versioni <strong>di</strong> <strong>Bash</strong> la richiedono.<br />

Nota: Il comando test è un builtin <strong>Bash</strong> che verifica i tipi <strong>di</strong> file e confronta le stringhe. Di<br />

conseguenza, in uno script <strong>Bash</strong>, test non richiama l’eseguibile esterno /usr/bin/test, che fa<br />

parte del pacchetto sh-utils. In modo analogo, [ non chiama /usr/bin/[, che è un link a<br />

/usr/bin/test.<br />

bash$ type test<br />

test is a shell builtin<br />

bash$ type ’[’<br />

[ is a shell builtin<br />

bash$ type ’[[’<br />

[[ is a shell keyword<br />

bash$ type ’]]’<br />

]] is a shell keyword<br />

bash$ type ’]’<br />

bash: type: ]: not found<br />

Esempio 7-2. Equivalenza <strong>di</strong> test, /usr/bin/test, [ ] e /usr/bin/[<br />

#!/bin/bash<br />

echo<br />

if test -z "$1"<br />

then<br />

echo "Nessun argomento da riga <strong>di</strong> comando."<br />

else<br />

echo "Il primo argomento da riga <strong>di</strong> comando è $1."<br />

fi<br />

echo<br />

if /usr/bin/test -z "$1" # Stesso risultato del builtin "test".<br />

then<br />

echo "Nessun argomento da riga <strong>di</strong> comando."<br />

else<br />

echo "Il primo argomento da riga <strong>di</strong> comando è $1."<br />

fi<br />

67

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

Saved successfully!

Ooh no, something went wrong!