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 33. Miscellanea<br />

# Imposta la variabile<br />

#+ a tutti gli elementi, separati da spazi, dell’array originario.<br />

#<br />

# È da notare che cercare <strong>di</strong> passare semplicemente l’array non funziona.<br />

# Ed ecco il trucco che permette <strong>di</strong> ottenere un array come "valore <strong>di</strong> ritorno".<br />

# *********************************************<br />

array_restituito=( ‘Passa_Array "$argomento"‘ )<br />

# *********************************************<br />

# Assegna l’output ’visualizzato’ della funzione all’array.<br />

echo "array restituito = ${array_restituito[@]}"<br />

echo "============================================================="<br />

# Ora, altra prova, un tentativo <strong>di</strong> accesso all’array (per elencarne<br />

#+ gli elementi) dall’esterno della funzione.<br />

Passa_Array "$argomento"<br />

# La funzione, <strong>di</strong> per sé, elenca l’array, ma...<br />

#+ non è consentito accedere all’array al <strong>di</strong> fuori della funzione.<br />

echo "Array passato (nella funzione) = ${array_passato[@]}"<br />

# VALORE NULL perché è una variabile locale alla funzione.<br />

echo<br />

exit 0<br />

Per un <strong>di</strong>mostrazione più elaborata <strong>di</strong> passaggio <strong>di</strong> array a funzioni, ve<strong>di</strong> Esempio A-10.<br />

• Utilizzando il costrutto doppie parentesi è possibile l’impiego della sintassi in stile C per impostare ed<br />

incrementare le variabili, e per i cicli for e while. Ve<strong>di</strong> Esempio 10-12 e Esempio 10-17.<br />

• Impostare path e umask all’inizio <strong>di</strong> uno script lo rende maggiormente “portabile” -- più probabilità<br />

che possa essere eseguito su una macchina “forestiera” il cui utente potrebbe aver combinato dei<br />

pasticci con $PATH e umask.<br />

#!/bin/bash<br />

PATH=/bin:/usr/bin:/usr/local/bin ; export PATH<br />

umask 022 # I file creati dallo script avranno i permessi impostati a 755.<br />

# Grazie a Ian D. Allen per il suggerimento.<br />

• Un’utile tecnica <strong>di</strong> <strong>scripting</strong> è quella <strong>di</strong> fornire ripetitivamente l’output <strong>di</strong> un filtro (con una pipe) allo<br />

stesso filtro, ma con una serie <strong>di</strong>versa <strong>di</strong> argomenti e/o <strong>di</strong> opzioni. tr e grep sono particolarmente adatti<br />

a questo scopo.<br />

Dall’esempio "wstrings.sh".<br />

wlist=‘strings "$1" | tr A-Z a-z | tr ’[:space:]’ Z | \<br />

580

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

Saved successfully!

Ooh no, something went wrong!