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 9. Variabili riviste<br />

# (Grazie a Stephane Chazelas, per aver chiarito il comportamento precedente.)<br />

# Un altro metodo è quello della notazione ${!t}, trattato nella<br />

#+ sezione "<strong>Bash</strong>, versione 2". Ve<strong>di</strong> anche ex78.sh.<br />

exit 0<br />

Qual’è l’utilità pratica della referenziazione in<strong>di</strong>retta delle variabili? Fornire a <strong>Bash</strong> un po’ delle<br />

funzionalità dei puntatori del C, ad esempio, nella ricerca nelle tabelle. Nonché avere qualche altra<br />

inreressantissima applicazione. . . .<br />

Nils Radtke mostra come realizzare nomi <strong>di</strong> variabili “<strong>di</strong>namici” e valutarne il contenuto. Questo può<br />

risultare utile quando occorre “includere” dei file <strong>di</strong> configurazione con source.<br />

#!/bin/bash<br />

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

# Questo file può essere "caricato" da un altro file tramite "source".<br />

isdnMioProviderReteRemota=172.16.0.100<br />

isdnTuoProviderReteRemota=10.0.0.10<br />

isdnServizioOnline="MioProvider"<br />

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

reteRemota=$(eval "echo \$$(echo isdn${isdnServizioOnline}ReteRemota)")<br />

reteRemota=$(eval "echo \$$(echo isdnMioProviderReteRemota)")<br />

reteRemota=$(eval "echo \$isdnMioProviderReteRemota")<br />

reteRemota=$(eval "echo $isdnMioProviderReteRemota")<br />

echo "$reteRemota" # 172.16.0.100<br />

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

# E fa ancor meglio.<br />

# Considerate il frammento seguente dove viene inizializzata una<br />

#+ variabile <strong>di</strong> nome getSparc, ma manca getIa64:<br />

verMirrorArch () {<br />

arch="$1";<br />

if [ "$(eval "echo \${$(echo get$(echo -ne $arch |<br />

sed ’s/^\(.\).*/\1/g’ | tr ’a-z’ ’A-Z’; echo $arch |<br />

sed ’s/^.\(.*\)/\1/g’)):-falso}")" = vero ]<br />

then<br />

return 0;<br />

else<br />

return 1;<br />

fi;<br />

}<br />

143

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

Saved successfully!

Ooh no, something went wrong!