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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

#> Alternativa.<br />

while [ "$var1" != "fine" ] #> while test "$var1" != "fine"<br />

Capitolo 33. Miscellanea<br />

• Un uso particolarmente intelligente dei costrutti if-test è quello per commentare blocchi <strong>di</strong> co<strong>di</strong>ce.<br />

#!/bin/bash<br />

BLOCCO_DI_COMMENTO=<br />

# Provate a impostare la variabile precedente ad un valore qualsiasi<br />

#+ ed otterrete una spiacevole sorpresa.<br />

if [ $BLOCCO_DI_COMMENTO ]; then<br />

Commento --<br />

================================================<br />

Questa è una riga <strong>di</strong> commento.<br />

Questa è un’altra riga <strong>di</strong> commento.<br />

Questa è un’altra riga ancora <strong>di</strong> commento.<br />

================================================<br />

echo "Questo messaggio non verrà visualizzato."<br />

I blocchi <strong>di</strong> commento non generano errori! Wow!<br />

fi<br />

echo "Niente più commenti, prego."<br />

exit 0<br />

Si confronti questo esempio con commentare un blocco <strong>di</strong> co<strong>di</strong>ce con gli here document.<br />

• L’uso della variabile <strong>di</strong> exit status $? consente allo script <strong>di</strong> verificare se un parametro contiene solo<br />

delle cifre, così che possa essere trattato come un intero.<br />

#!/bin/bash<br />

SUCCESSO=0<br />

E_ERR_INPUT=65<br />

test "$1" -ne 0 -o "$1" -eq 0 2>/dev/null<br />

# Un intero è o <strong>di</strong>verso da 0 o uguale a 0.<br />

# 2>/dev/null sopprime il messaggio d’errore.<br />

if [ $? -ne "$SUCCESSO" ]<br />

576

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

Saved successfully!

Ooh no, something went wrong!