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.

# ----<br />

# Gli enunciati "declare -a" alle righe 31 e 43<br />

#+ non sarebbero strettamente necessari perch�sono impliciti<br />

#+ nell’assegnamento nella forma Array=( ... ).<br />

# Tuttavia, l’eliminazione <strong>di</strong> queste <strong>di</strong>chiarazioni rallenta<br />

#+ l’esecuzione delle successive sezioni dello script.<br />

# Provate e vedete cosa succede.<br />

exit 0<br />

Capitolo 26. Array<br />

Nota: L’aggiunta del superfluo enunciato declare -a nella <strong>di</strong>chiarazione <strong>di</strong> un array può velocizzare<br />

l’esecuzione delle successive operazioni sullo stesso array.<br />

Esempio 26-9. Copiare e concatenare array<br />

#! /bin/bash<br />

# CopyArray.sh<br />

#<br />

# Script <strong>di</strong> Michael Zick.<br />

# Usato con il permesso dell’autore.<br />

# Come "Passare per Nome & restituire per Nome"<br />

#+ ovvero "Costruirsi il proprio enunciato <strong>di</strong> assegnamento".<br />

CpArray_Mac() {<br />

# Costruttore dell’enunciato <strong>di</strong> assegnamento<br />

echo -n ’eval ’<br />

echo -n "$2" # Nome <strong>di</strong> destinazione<br />

echo -n ’=( ${’<br />

echo -n "$1" # Nome <strong>di</strong> origine<br />

echo -n ’[@]} )’<br />

# Si sarebbe potuto fare con un solo comando.<br />

# E’ solo una questione <strong>di</strong> stile.<br />

}<br />

declare -f CopiaArray # Funzione "Puntatore"<br />

CopiaArray=CpArray_Mac # Costruttore dell’ennuciato<br />

Enfatizza()<br />

{<br />

# Enfatizza l’array <strong>di</strong> nome $1.<br />

# (Lo sposa all’array contenente "veramente fantastico".)<br />

# Risultato nell’array <strong>di</strong> nome $2.<br />

local -a TMP<br />

485

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

Saved successfully!

Ooh no, something went wrong!