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 10. Cicli ed alternative<br />

# "-d" o "--debug" parametro?<br />

DEBUG=1<br />

;;<br />

-c|--conf)<br />

FILECONF="$2"<br />

shift<br />

if [ ! -f $FILECONF ]; then<br />

echo "Errore: il file in<strong>di</strong>cato non esiste!"<br />

exit $E_ERR_FILECONF # Errore <strong>di</strong> file non trovato.<br />

fi<br />

;;<br />

esac<br />

shift # Verifica la serie successiva <strong>di</strong> parametri.<br />

done<br />

# Dallo script "Log2Rot" <strong>di</strong> Stefano Falsetto,<br />

#+ parte del suo pacchetto "rottlog".<br />

# Usato con il consenso dell’autore.<br />

Esempio 10-26. Usare la sostituzione <strong>di</strong> comando per creare la variabile <strong>di</strong> case<br />

#!/bin/bash<br />

# case-cmd.sh: usare la sostituzione <strong>di</strong> comando per creare la variabile<br />

#+ <strong>di</strong> "case".<br />

case $( arch ) in # "arch" restituisce l’architettura della macchina.<br />

# Equivale a ’uname -m’...<br />

i386 ) echo "Macchina con processore 80386";;<br />

i486 ) echo "Macchina con processore 80486";;<br />

i586 ) echo "Macchina con processore Pentium";;<br />

i686 ) echo "Macchina con processore Pentium2+";;<br />

* ) echo "Altro tipo <strong>di</strong> macchina";;<br />

esac<br />

exit 0<br />

Un costrutto case può filtrare le stringhe in una ricerca che fa uso del globbing.<br />

Esempio 10-27. Una semplice ricerca <strong>di</strong> stringa<br />

#!/bin/bash<br />

# match-string.sh: semplice ricerca <strong>di</strong> stringa<br />

verifica_stringa ()<br />

{<br />

UGUALE=0<br />

NONUGUALE=90<br />

PARAM=2 # La funzione richiede 2 argomenti.<br />

ERR_PARAM=91<br />

[ $# -eq $PARAM ] || return $ERR_PARAM<br />

181

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

Saved successfully!

Ooh no, something went wrong!