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.

Capitolo 23. Funzioni<br />

# |_______| | | | |<br />

# |_________| | | | |<br />

# |___________| | | | |<br />

# | | | | | |<br />

# .--------------------------------------------------------------.<br />

# |**************************************************************|<br />

# #1 #2 #3<br />

#<br />

#========================================================================#<br />

E_NOPARAM=66 # Nessun parametro passato allo script.<br />

E_ERR_PARAM=67 # Il numero <strong>di</strong> <strong>di</strong>schi passato allo script non è valido.<br />

Mosse= # Variabile globale contenente il numero degli spostamenti.<br />

# Mo<strong>di</strong>fiche allo script originale.<br />

eseguehanoi() { # Funzione ricorsiva.<br />

case $1 in<br />

0)<br />

;;<br />

*)<br />

eseguehanoi "$(($1-1))" $2 $4 $3<br />

echo sposto $2 "-->" $3<br />

let "Mosse += 1" # Mo<strong>di</strong>fica allo script originale.<br />

eseguehanoi "$(($1-1))" $4 $3 $2<br />

;;<br />

esac<br />

}<br />

case $# in<br />

1)<br />

case $(($1>0)) in # Deve esserci almeno un <strong>di</strong>sco.<br />

1)<br />

eseguehanoi $1 1 3 2<br />

echo "Totale spostamenti = $Mosse"<br />

exit 0;<br />

;;<br />

*)<br />

echo "$0: numero <strong>di</strong> <strong>di</strong>schi non consentito";<br />

exit $E_ERR_PARAM;<br />

;;<br />

esac<br />

;;<br />

*)<br />

echo "utilizzo: $0 N"<br />

echo " Dove \"N\" è il numero dei <strong>di</strong>schi."<br />

exit $E_NOPARAM;<br />

;;<br />

esac<br />

# Esercizi:<br />

# ---------<br />

# 1) Eventuali coman<strong>di</strong> posti in questo punto verrebbero eseguiti?<br />

463

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

Saved successfully!

Ooh no, something went wrong!