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.

END {<br />

print totale<br />

}<br />

" "$nomefile"<br />

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

# Fine script awk.<br />

Capitolo 9. Variabili riviste<br />

# La referenziazione in<strong>di</strong>retta evita le <strong>di</strong>fficoltà della referenziazione<br />

#+ <strong>di</strong> una variabile <strong>di</strong> shell all’interno <strong>di</strong> uno script awk incorporato.<br />

# Grazie, Stephane Chazelas.<br />

exit 0<br />

Cautela<br />

Questo metodo è un po’ complicato. Se la seconda variabile mo<strong>di</strong>fica il proprio<br />

valore, allora la prima deve essere correttamente dereferenziata (come<br />

nell’esempio precedente). Fortunatamente, la notazione ${!variabile},<br />

introdotta con la versione 2 <strong>di</strong> <strong>Bash</strong> (ve<strong>di</strong> Esempio 34-2), rende la referenziazione<br />

in<strong>di</strong>retta più intuitiva.<br />

<strong>Bash</strong> non supporta l’aritmetica dei puntatori e ciò limita drasticamente l’utilità della<br />

referenziazione in<strong>di</strong>retta. Questa, infatti, in un linguaggio <strong>di</strong> <strong>scripting</strong>, è solo un brutto<br />

espe<strong>di</strong>ente.<br />

9.6. $RANDOM: genera un intero casuale<br />

$RANDOM è una funzione interna <strong>di</strong> <strong>Bash</strong> (non una costante) che restituisce un intero pseudocasuale 5<br />

nell’intervallo 0 - 32767. Non dovrebbe essere utilizzata per generare una chiave <strong>di</strong> cifratura.<br />

Esempio 9-24. Generare numeri casuali<br />

#!/bin/bash<br />

# $RANDOM restituisce un intero casuale <strong>di</strong>verso ad ogni chiamata.<br />

# Intervallo nominale: 0 - 32767 (intero con segno <strong>di</strong> 16-bit).<br />

NUM_MASSIMO=10<br />

contatore=1<br />

echo<br />

echo "$NUM_MASSIMO numeri casuali:"<br />

echo "-----------------"<br />

while [ "$contatore" -le $NUM_MASSIMO ] # Genera 10 ($NUM_MASSIMO)<br />

145

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

Saved successfully!

Ooh no, something went wrong!