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.

-z<br />

-n<br />

è maggiore <strong>di</strong>, in or<strong>di</strong>ne alfabetico ASCII<br />

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

if [ "$a" \> "$b" ]<br />

Si noti che “>” necessita dell’escaping nel costrutto [ ].<br />

Ve<strong>di</strong> Esempio 26-11 per un’applicazione <strong>di</strong> questo operatore <strong>di</strong> confronto.<br />

la stringa è “nulla”, cioè, ha lunghezza zero<br />

la stringa non è “nulla”.<br />

Cautela<br />

L’operatore -n richiede assolutamente il quoting della stringa all’interno<br />

delle parentesi quadre. L’utilizzo, tra le parentesi quadre, <strong>di</strong> una stringa<br />

senza quoting, sia con ! -z che da sola (ve<strong>di</strong> Esempio 7-6),<br />

normalmente funziona, tuttavia non è una pratica sicura. Bisogna<br />

sempre utilizzare il quoting su una stringa da verificare. 3<br />

Esempio 7-5. Confronti numerici e <strong>di</strong> stringhe<br />

#!/bin/bash<br />

a=4<br />

b=5<br />

Capitolo 7. Verifiche<br />

# Qui "a" e "b" possono essere trattate sia come interi che come stringhe.<br />

# Ci si può facilmente confondere tra i confronti numerici e quelli sulle<br />

#+ stringhe, perché le variabili <strong>Bash</strong> non sono tipizzate.<br />

#<br />

# <strong>Bash</strong> consente le operazioni <strong>di</strong> interi e il confronto <strong>di</strong> variabili<br />

#+ il cui valore è composto solamente da cifre.<br />

# Comunque attenzione, siete avvisati.<br />

echo<br />

if [ "$a" -ne "$b" ]<br />

77

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

Saved successfully!

Ooh no, something went wrong!