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 />

parola=Linux<br />

sequenza_lettere=inu<br />

if echo "$parola" | grep -q "$sequenza_lettere"<br />

# L’opzione "-q" <strong>di</strong> grep elimina l’output.<br />

then<br />

echo "$sequenza_lettere trovata in $parola"<br />

else<br />

echo "$sequenza_lettere non trovata in $parola"<br />

fi<br />

if COMANDO_CON_EXIT_STATUS_0_SE_NON_SI_VERIFICA_UN_ERRORE<br />

then echo "Comando eseguito."<br />

else echo "Comando fallito."<br />

fi<br />

• Un costrutto if/then può contenere confronti e verifiche annidate.<br />

if echo "Il prossimo *if* è parte del costrutto del primo *if*."<br />

if [[ $confronto = "intero" ]]<br />

then (( a < b ))<br />

else<br />

[[ $a < $b ]]<br />

fi<br />

then<br />

echo ’$a è inferiore a $b’<br />

fi<br />

Dettagliata spiegazione della “con<strong>di</strong>zione-if” cortesia <strong>di</strong> Stéphane Chazelas.<br />

Esempio 7-1. Cos’è vero?<br />

#!/bin/bash<br />

# Suggerimento:<br />

# se non siete sicuri <strong>di</strong> come certe con<strong>di</strong>zioni verranno valutate,<br />

#+ controllatele con una verifica if.<br />

echo<br />

echo "Verifica \"0\""<br />

if [ 0 ] # zero<br />

then<br />

echo "0 è vero."<br />

else<br />

Capitolo 7. Verifiche<br />

63

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

Saved successfully!

Ooh no, something went wrong!