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.

fi<br />

Appen<strong>di</strong>ce C. Una breve introduzione a Sed e Awk<br />

if [ -z "$2" ] ; then<br />

echo "$2: Non è stata specificata nessuna lettera." 2>&1<br />

utilizzo<br />

fi<br />

shift # Le lettere sono state specificate.<br />

for lettera in ‘echo $@‘ # Per ognuna . . .<br />

do<br />

INIT_TAB_AWK="$INIT_TAB_AWK tab_search[${conteggio}] = \"$lettera\";\<br />

final_tab[${conteggio}] = 0; "<br />

# Passato come parametro al successivo script awk.<br />

conteggio=‘expr $conteggio + 1‘<br />

done<br />

# DEBUGGING:<br />

# echo $INIT_TAB_AWK;<br />

cat $FILE_INDICATO |<br />

# Il file viene collegato, per mezzo <strong>di</strong> una pipe, al seguente script awk.<br />

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

# La versione precedente dello script usava:<br />

# awk -v tab_search=0 -v final_tab=0 -v tab=0 -v nb_letter=0 -v chara=0 -v chara2=0 \<br />

awk \<br />

"BEGIN { $INIT_TAB_AWK } \<br />

{ split(\$0, tab, \"\"); \<br />

for (chara in tab) \<br />

{ for (chara2 in tab_search) \<br />

{ if (tab_search[chara2] == tab[chara]) { final_tab[chara2]++ } } } } \<br />

END { for (chara in final_tab) \<br />

{ print tab_search[chara] \" => \" final_tab[chara] } }"<br />

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

# Niente <strong>di</strong> così complicato, solo . . .<br />

#+ cicli for, costrutti if e un paio <strong>di</strong> funzioni specializzate.<br />

exit $?<br />

# Confrontate questo script con letter-count.sh.<br />

Per <strong>di</strong>mostrazioni più semplici dell’uso <strong>di</strong> awk negli script <strong>di</strong> shell, ve<strong>di</strong>:<br />

1. Esempio 11-12<br />

2. Esempio 16-8<br />

3. Esempio 12-29<br />

4. Esempio 33-5<br />

773

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

Saved successfully!

Ooh no, something went wrong!