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.

}<br />

case "$1" in<br />

"$2") return $UGUALE;;<br />

* ) return $NONUGUALE;;<br />

esac<br />

a=uno<br />

b=due<br />

c=tre<br />

d=due<br />

verifica_stringa $a # numero <strong>di</strong> parametri errato<br />

echo $? # 91<br />

verifica_stringa $a $b # <strong>di</strong>verse<br />

echo $? # 90<br />

verifica_stringa $b $d # uguali<br />

echo $? # 0<br />

exit 0<br />

Esempio 10-28. Verificare un input alfabetico<br />

Capitolo 10. Cicli ed alternative<br />

#!/bin/bash<br />

# isalpha.sh: Utilizzare la struttura "case" per filtrare una stringa.<br />

SUCCESSO=0<br />

FALLIMENTO=-1<br />

isalpha () # Verifica se il *primo carattere* della stringa<br />

#+ <strong>di</strong> input è una lettera.<br />

{<br />

if [ -z "$1" ] # Nessun argomento passato?<br />

then<br />

return $FALLIMENTO<br />

fi<br />

case "$1" in<br />

[a-zA-Z]*) return $SUCCESSO;; # Inizia con una lettera?<br />

* ) return $FALLIMENTO;;<br />

esac<br />

} # Confrontatelo con la funzione "isalpha ()" del C.<br />

isalpha2 () # Verifica se l’*intera stringa* è composta da lettere.<br />

{<br />

182

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

Saved successfully!

Ooh no, something went wrong!