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.

exit 0<br />

Capitolo 23. Funzioni<br />

La domanda logica successiva è se i parametri possono essere dereferenziati dopo essere stati passati alla<br />

funzione.<br />

Esempio 23-5. Dereferenziare un parametro passato a una funzione<br />

#!/bin/bash<br />

# dereference.sh<br />

# Dereferenziare un parametro passato ad una funzione.<br />

# Script <strong>di</strong> Bruce W. Clare.<br />

dereferenzia ()<br />

{<br />

y=\$"$1" # Nome della variabile.<br />

echo $y # $Prova<br />

}<br />

x=‘eval "expr \"$y\" "‘<br />

echo $1=$x<br />

eval "$1=\"Un testo <strong>di</strong>verso \"" # Assegna un nuovo valore.<br />

Prova="Un testo"<br />

echo $Prova "prima" # Un testo prima<br />

dereferenzia Prova<br />

echo $Prova "dopo" # Un testo <strong>di</strong>verso dopo<br />

exit 0<br />

Esempio 23-6. Ancora, dereferenziare un parametro passato a una funzione<br />

#!/bin/bash<br />

# ref-params.sh: Dereferenziare un parametro passato a una funzione.<br />

# (Esempio complesso)<br />

ITERAZIONI=3 # Numero <strong>di</strong> input da immettere.<br />

contai=1<br />

lettura () {<br />

# Richiamata nella forma lettura nomevariabile,<br />

#+ visualizza il dato precedente tra parentesi quadre come dato predefinito,<br />

#+ quin<strong>di</strong> chiede un nuovo valore.<br />

local var_locale<br />

echo -n "Inserisci un dato "<br />

eval ’echo -n "[$’$1’] "’ # Dato precedente.<br />

# eval echo -n "[\$$1] " # Più facile da capire,<br />

#+ ma si perde lo spazio finale al prompt.<br />

450

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

Saved successfully!

Ooh no, something went wrong!