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.

echo ${area[6]}<br />

# Questo assegnamento fallisce perché non è permesso sommare<br />

#+ un intero con una stringa.<br />

echo; echo; echo<br />

Capitolo 26. Array<br />

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

# Un altro array, "area2".<br />

# Metodo <strong>di</strong> assegnamento alternativo...<br />

# nome_array=( XXX YYY ZZZ ... )<br />

area2=( zero uno due tre quattro )<br />

echo -n "area2[0] = "<br />

echo ${area2[0]}<br />

# Aha, in<strong>di</strong>cizzazione in base zero (il primo elemento dell’array<br />

#+ è [0], non [1]).<br />

echo -n "area2[1] = "<br />

echo ${area2[1]} # [1] è il secondo elemento dell’array.<br />

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

echo; echo; echo<br />

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

# Ancora un altro array, "area3".<br />

# Ed un’altra modalità ancora <strong>di</strong> assegnamento...<br />

# nome_array=([xx]=XXX [yy]=YYY ...)<br />

area3=([17]=<strong>di</strong>ciassette [24]=ventiquattro)<br />

echo -n "area3[17] = "<br />

echo ${area3[17]}<br />

echo -n "area3[24] = "<br />

echo ${area3[24]}<br />

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

exit 0<br />

Nota: <strong>Bash</strong> consente le operazioni sugli arrray anche se questi non sono stati <strong>di</strong>chiarati tali<br />

esplicitamente.<br />

stringa=abcABC123ABCabc<br />

echo ${stringa[@]} # abcABC123ABCabc<br />

echo ${stringa[*]} # abcABC123ABCabc<br />

echo ${stringa[0]} # abcABC123ABCabc<br />

echo ${stringa[1]} # Nessun output!<br />

# Perché?<br />

echo ${#stringa[@]} # 1<br />

# Array <strong>di</strong> un solo elemento.<br />

# La stringa stessa.<br />

# Grazie a Michael Zick per la precisazione.<br />

473

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

Saved successfully!

Ooh no, something went wrong!