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.

}<br />

Capitolo 23. Funzioni<br />

# Esercizio:<br />

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

# Spiegate come opera la funzione.<br />

# Suggerimento: <strong>di</strong>visione per mezzo <strong>di</strong> sottrazioni successive.<br />

calcola_romano $num 100 C<br />

num=$?<br />

calcola_romano $num 90 LXXXX<br />

num=$?<br />

calcola_romano $num 50 L<br />

num=$?<br />

calcola_romano $num 40 XL<br />

num=$?<br />

calcola_romano $num 10 X<br />

num=$?<br />

calcola_romano $num 9 IX<br />

num=$?<br />

calcola_romano $num 5 V<br />

num=$?<br />

calcola_romano $num 4 IV<br />

num=$?<br />

calcola_romano $num 1 I<br />

echo<br />

exit 0<br />

Ve<strong>di</strong> anche Esempio 10-28.<br />

Importante: Il più grande intero positivo che una funzione può restituire è 255. Il comando<br />

return è strettamente legato al concetto <strong>di</strong> exit status, e ciò è la causa <strong>di</strong> questa particolare<br />

limitazione. Fortunatamente, esistono <strong>di</strong>versi espe<strong>di</strong>enti per quelle situazioni che richiedono,<br />

come valore <strong>di</strong> ritorno della funzione, un intero maggiore <strong>di</strong> 255.<br />

Esempio 23-9. Verificare valori <strong>di</strong> ritorno <strong>di</strong> gran<strong>di</strong> <strong>di</strong>mensioni in una funzione<br />

#!/bin/bash<br />

# return-test.sh<br />

# Il maggiore valore positivo che una funzione può restituire è 255.<br />

val_ritorno () # Restituisce tutto quello che gli viene passato.<br />

{<br />

return $1<br />

}<br />

val_ritorno 27 # o.k.<br />

echo $? # Restituisce 27.<br />

val_ritorno 255 # Ancora o.k.<br />

echo $? # Restituisce 255.<br />

454

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

Saved successfully!

Ooh no, something went wrong!