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.

while [con<strong>di</strong>zione]<br />

do<br />

comando...<br />

done<br />

Capitolo 10. Cicli ed alternative<br />

Come nel caso dei cicli for, collocare il do sulla stessa riga della con<strong>di</strong>zione <strong>di</strong> verifica rende<br />

necessario l’uso del punto e virgola.<br />

while [con<strong>di</strong>zione] ; do<br />

È da notare che alcuni cicli while specializzati, come per esempio il costrutto getopts, si <strong>di</strong>scostano<br />

un po’ dalla struttura standard appena illustrata.<br />

Esempio 10-14. Un semplice ciclo while<br />

#!/bin/bash<br />

var0=0<br />

LIMITE=10<br />

while [ "$var0" -lt "$LIMITE" ]<br />

do<br />

echo -n "$var0 " # -n sopprime il ritorno a capo.<br />

# ^ Lo spazio serve a separare i numeri visualizzati.<br />

var0=‘expr $var0 + 1‘ # var0=$(($var0+1)) anche questa forma va bene.<br />

# var0=$((var0 + 1)) anche questa forma va bene.<br />

# let "var0 += 1" anche questa forma va bene.<br />

done # Anche vari altri meto<strong>di</strong> funzionano.<br />

echo<br />

exit 0<br />

Esempio 10-15. Un altro ciclo while<br />

#!/bin/bash<br />

echo<br />

# Equivalente a:<br />

while [ "$var1" != "fine" ]<br />

do<br />

# while test "$var1" != "fine"<br />

echo "Immetti la variabile #1 (fine per terminare) "<br />

read var1 # Non ’read $var1’ (perché?).<br />

echo "variabile #1 = $var1" # È necessario il "quoting"<br />

169

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

Saved successfully!

Ooh no, something went wrong!