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.

then<br />

echo "$a non è uguale a $b"<br />

echo "(confronto numerico)"<br />

fi<br />

echo<br />

if [ "$a" != "$b" ]<br />

then<br />

echo "$a non è uguale a $b."<br />

echo "(confronto <strong>di</strong> stringhe)"<br />

# "4" != "5"<br />

# ASCII 52 != ASCII 53<br />

fi<br />

# In questo particolare esempio funziona sia "-ne" che "!=".<br />

echo<br />

exit 0<br />

Esempio 7-6. Verificare se una stringa è nulla<br />

Capitolo 7. Verifiche<br />

#!/bin/bash<br />

# str-test.sh: Verifica <strong>di</strong> stringhe nulle e <strong>di</strong> stringhe senza quoting (*)<br />

# Utilizzando if [ ... ]<br />

# Se una stringa non è stata inizializzata, non ha un valore definito.<br />

# Questo stato si <strong>di</strong>ce "nullo" (non zero!).<br />

if [ -n $stringa1 ] # $stringa1 non è stata <strong>di</strong>chiarata o inizializzata.<br />

then<br />

echo "La stringa \"stringa1\" non è nulla."<br />

else<br />

echo "La stringa \"stringa1\" è nulla."<br />

fi<br />

# Risultato sbagliato.<br />

# Viene visualizzato $stringa1 come non nulla, anche se non era inizializzata.<br />

echo<br />

# Proviamo ancora.<br />

if [ -n "$stringa1" ] # Questa volta è stato applicato il quoting a $stringa1.<br />

then<br />

echo "la stringa \"stringa1\" non è nulla."<br />

else<br />

echo "La stringa \"stringa1\" è nulla."<br />

78

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

Saved successfully!

Ooh no, something went wrong!