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.

Capitolo 8. Operazioni ed argomenti correlati<br />

# Notate che l’operatore || non è consentito nel costrutto [ ... ].<br />

Nota: <strong>Bash</strong> verifica l’exit status <strong>di</strong> ogni enunciato collegato con un operatore logico.<br />

Esempio 8-3. Con<strong>di</strong>zioni <strong>di</strong> verifica composte utilizzando && e ||<br />

#!/bin/bash<br />

a=24<br />

b=47<br />

if [ "$a" -eq 24 ] && [ "$b" -eq 47 ]<br />

then<br />

echo "Verifica nr.1 eseguita con successo."<br />

else<br />

echo "Verifica nr.1 fallita."<br />

fi<br />

# ERRORE: if [ "$a" -eq 24 && "$b" -eq 47 ]<br />

#+ cerca <strong>di</strong> eseguire ’ [ "$a" -eq 24 ’<br />

#+ e fallisce nella ricerca <strong>di</strong> corrispondenza <strong>di</strong> ’]’.<br />

#<br />

# Nota: if [[ $a -eq 24 && $b -eq 24 ]] funziona<br />

# La verifica if con le doppie parentesi quadre è più flessibile<br />

#+ della versione con le paretesi quadre singole.<br />

# ("&&" ha un significato <strong>di</strong>verso nella riga 17 <strong>di</strong> quello della riga 6.).<br />

# Grazie a Stephane Chazelas per averlo evidenziato.<br />

if [ "$a" -eq 98 ] || [ "$b" -eq 47 ]<br />

then<br />

echo "Verifica nr.2 eseguita con successo."<br />

else<br />

echo "Verifica nr.2 fallita."<br />

fi<br />

# Le opzioni -a e -o offrono<br />

#+ una con<strong>di</strong>zione <strong>di</strong> verifica composta alternativa.<br />

# Grazie a Patrick Callahan per la precisazione.<br />

if [ "$a" -eq 24 -a "$b" -eq 47 ]<br />

then<br />

echo "Verifica nr.3 eseguita con successo."<br />

else<br />

echo "Verifica nr.3 fallita."<br />

fi<br />

90

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

Saved successfully!

Ooh no, something went wrong!