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.

while<br />

---<br />

Adesso un ciclo for impiegato in un’applicazione “pratica”.<br />

Esempio 10-13. Utilizzare efax in modalità batch<br />

#!/bin/bash<br />

# Inviare un fax (dovete avere un ’fax’ installato)<br />

ARG_ATTESI=2<br />

E_ERR_ARG=65<br />

if [ $# -ne $ARG_ATTESI ]<br />

# Verifica il corretto numero <strong>di</strong> argomenti.<br />

then<br />

echo "Utilizzo: ‘basename $0‘ nr_telefono file_testo"<br />

exit $E_ERR_ARG<br />

fi<br />

if [ ! -f "$2" ]<br />

then<br />

echo "Il file $2 non è un file <strong>di</strong> testo"<br />

exit $E_ERR_ARG<br />

fi<br />

Capitolo 10. Cicli ed alternative<br />

fax make $2 # Crea file fax formattati dai file <strong>di</strong> testo.<br />

for file in $(ls $2.0*) # Concatena i file appena creati.<br />

# Usa il carattere jolly in lista.<br />

do<br />

fil="$fil $file"<br />

done<br />

efax -d /dev/ttyS3 -o1 -t "T$1" $fil # Esegue il lavoro.<br />

# Come ha sottolineato S.C. il ciclo for potrebbe essere sostituito con<br />

# efax -d /dev/ttyS3 -o1 -t "T$1" $2.0*<br />

# ma non sarebbe stato altrettanto istruttivo [sorriso].<br />

exit 0<br />

Questo costrutto verifica una con<strong>di</strong>zione data all’inizio del ciclo che viene mantenuto in esecuzione<br />

finché quella con<strong>di</strong>zione rimane vera (restituisce exit status 0). A <strong>di</strong>fferenza del ciclo for, il ciclo<br />

while viene usato in quelle situazioni in cui il numero delle iterazioni non è conosciuto in anticipo.<br />

168

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

Saved successfully!

Ooh no, something went wrong!