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.

exit 0<br />

Il precedente esempio può essere mo<strong>di</strong>ficato per re<strong>di</strong>rigere anche l’output del ciclo.<br />

Esempio 16-9. Ciclo for re<strong>di</strong>retto (re<strong>di</strong>retti sia lo st<strong>di</strong>n che lo stdout)<br />

#!/bin/bash<br />

Capitolo 16. Re<strong>di</strong>rezione I/O<br />

if [ -z "$1" ]<br />

then<br />

Nomefile=nomi.data # È il file predefinito, se non ne viene<br />

#+ specificato alcuno.<br />

else<br />

Nomefile=$1<br />

fi<br />

Filereg=$Nomefile.nuovo # Nome del file in cui vengono salvati i<br />

#+ risultati.<br />

NomeFinale=Jonah # Nome per terminare la "lettura".<br />

conta_righe=‘wc $Nomefile | awk ’{ print $1 }’‘ # Numero <strong>di</strong> righe del file<br />

#+ in<strong>di</strong>cato.<br />

for nome in ‘seq $conta_righe‘<br />

do<br />

read nome<br />

echo "$nome"<br />

if [ "$nome" = "$NomeFinale" ]<br />

then<br />

break<br />

fi<br />

done < "$Nomefile" > "$Filereg" # Re<strong>di</strong>rige lo st<strong>di</strong>n nel file $Nomefile,<br />

# ^^^^^^^^^^^^^^^^^^^^^^^^^^ e lo salva nel file <strong>di</strong> backup $Filereg.<br />

exit 0<br />

Esempio 16-10. Costrutto if/then re<strong>di</strong>retto<br />

#!/bin/bash<br />

if [ -z "$1" ]<br />

then<br />

Nomefile=nomi.data # È il file predefinito, se non ne viene<br />

#+ specificato alcuno.<br />

else<br />

Nomefile=$1<br />

fi<br />

TRUE=1<br />

if [ "$TRUE" ] # vanno bene anche if true e if :<br />

then<br />

405

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

Saved successfully!

Ooh no, something went wrong!