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.

#+ <strong>di</strong> comando.<br />

then<br />

echo "Utilizzo: ‘basename $0‘ nomefile numero-colonna"<br />

exit $E_ERR_ARG<br />

fi<br />

nomefile=$1<br />

numero_colonna=$2<br />

Capitolo 33. Miscellanea<br />

# Il passaggio <strong>di</strong> variabili <strong>di</strong> shell allo script awk incorporato<br />

#+ è un po’ complicato.<br />

# Un metodo consiste nell’applicare il quoting forte alla variabile dello<br />

#+ script <strong>Bash</strong> all’interno dello script awk.<br />

# $’$VAR_SCRIPT_BASH’<br />

# ^ ^<br />

# È ciò che è stato fatto nello script awk incorporato che segue.<br />

# Vedete la documentazione awk per maggiori dettagli.<br />

# Uno script awk che occupa più righe viene invocato con: awk ’ ..... ’<br />

# Inizio dello script awk.<br />

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

’<br />

{ totale += $’"${numero_colonna}"’<br />

}<br />

END {<br />

print totale<br />

}<br />

’ "$nomefile"<br />

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

# Fine dello script awk.<br />

# Potrebbe non essere sicuro passare variabili <strong>di</strong> shell a uno script awk<br />

#+ incorporato, così Stephane Chazelas propone la seguente alternativa:<br />

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

# awk -v numero_colonna="$numero_colonna" ’<br />

# { totale += $numero_colonna<br />

# }<br />

# END {<br />

# print totale<br />

# }’ "$nomefile"<br />

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

exit 0<br />

Per quegli script che necessitano <strong>di</strong> un unico strumento tuttofare, un coltellino svizzero informatico,<br />

esiste Perl. Perl combina le capacità <strong>di</strong> sed e awk, e, per <strong>di</strong> più, un’ampia parte <strong>di</strong> quelle del C. È<br />

550

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

Saved successfully!

Ooh no, something went wrong!