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.

un-parts<br />

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

# sh ex33a.sh -dXYZ<br />

# sh ex33a.sh -d XYZ<br />

# sh ex33a.sh -abcd<br />

# sh ex33a.sh -abcdZ<br />

# sh ex33a.sh -z<br />

# sh ex33a.sh a<br />

# Spiegate i risultati <strong>di</strong> ognuna delle precedenti esecuzioni.<br />

E_ERR_OPZ=65<br />

if [ "$#" -eq 0 ]<br />

then # Lo script richiede almeno un argomento da riga <strong>di</strong> comando.<br />

echo "Utilizzo $0 -[opzioni a,b,c]"<br />

exit $E_ERR_OPZ<br />

fi<br />

set -- ‘getopt "abcd:" "$@"‘<br />

# Imposta come parametri posizionali gli argomenti passati da riga <strong>di</strong> comando.<br />

# Cosa succede se si usa "$*" invece <strong>di</strong> "$@"?<br />

while [ ! -z "$1" ]<br />

do<br />

case "$1" in<br />

-a) echo "Opzione \"a\"";;<br />

-b) echo "Opzione \"b\"";;<br />

-c) echo "Opzione \"c\"";;<br />

-d) echo "Opzione \"d\" $2";;<br />

*) break;;<br />

esac<br />

shift<br />

done<br />

# Solitamente in uno script è meglio usare il builtin ’getopts’,<br />

#+ piuttosto che ’getopt’.<br />

# Ve<strong>di</strong> "ex33.sh".<br />

exit 0<br />

Per una simulazione semplificata <strong>di</strong> getopt ve<strong>di</strong> Esempio 9-12.<br />

Il comando run-parts 9 esegue tutti gli script presenti nella <strong>di</strong>rectory <strong>di</strong> riferimento,<br />

sequenzialmente ed in or<strong>di</strong>ne alfabetico. Naturalmente gli script devono avere i permessi <strong>di</strong><br />

esecuzione.<br />

Il demone cron invoca run-parts per eseguire gli script presenti nelle <strong>di</strong>rectory /etc/cron.*<br />

330

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

Saved successfully!

Ooh no, something went wrong!