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.

{<br />

}<br />

echo "Funzione \"f2\"."<br />

Capitolo 23. Funzioni<br />

f1 # La funzione "f2", in realtà, viene chiamata solo a questo punto,<br />

#+ sebbene vi si faccia riferimento prima della sua definizione.<br />

# Questo è consentito.<br />

# Grazie, S.C.<br />

È anche possibile annidare una funzione in un’altra, sebbene non sia molto utile.<br />

f1 ()<br />

{<br />

}<br />

f2 () # annidata<br />

{<br />

echo "Funzione \"f2\", all’interno <strong>di</strong> \"f1\"."<br />

}<br />

f2 # Restituisce un messaggio d’errore.<br />

# Sarebbe inutile anche farla precedere da "declare -f f2".<br />

echo<br />

f1 # Non fa niente, perché richiamare"f1" non implica richiamare<br />

#+ automaticamente "f2".<br />

f2 # Ora è tutto a posto, "f2" viene eseguita, perché la sua<br />

#+ definizione è stata resa visibile tramite la chiamata <strong>di</strong> "f1".<br />

# Grazie, S.C.<br />

Le <strong>di</strong>chiarazioni <strong>di</strong> funzione possono comparire in posti impensati, anche dove dovrebbe trovarsi un<br />

comando.<br />

ls -l | foo() { echo "foo"; } # Consentito, ma inutile.<br />

if [ "$USER" = bozo ]<br />

then<br />

saluti_bozo () # Definizione <strong>di</strong> funzione inserita in un costrutto if/then.<br />

{<br />

echo "Ciao, Bozo."<br />

}<br />

fi<br />

446

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

Saved successfully!

Ooh no, something went wrong!