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.

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

Questi script, sebbene non siano stati inseriti nel testo del documento, illustrano alcune interessanti<br />

tecniche <strong>di</strong> programmazione <strong>di</strong> shell. Sono anche utili. Ci si <strong>di</strong>verta ad analizzarli e a eseguirli.<br />

Esempio A-1. mailformat: impaginare un messaggio e-mail<br />

#!/bin/bash<br />

# mail-format.sh (ver. 1.1): formatta messaggi e-mail.<br />

# Si sbarazza <strong>di</strong> accenti circonflessi, tabulazioni<br />

#+ e sud<strong>di</strong>vide anche le righe eccessivamente lunghe.<br />

# =====================================================================<br />

# Verifica standard del(gli) argomento(i) dello script<br />

ARG=1<br />

E_ERR_ARG=65<br />

E_NOFILE=66<br />

if [ $# -ne $ARG ] # Numero corretto <strong>di</strong> argomenti passati allo script?<br />

then<br />

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

exit $E_ERR_ARG<br />

fi<br />

if [ -f "$1" ] # Verifica l’esistenza del file.<br />

then<br />

nome_file=$1<br />

else<br />

echo "Il file \"$1\" non esiste."<br />

exit $E_NOFILE<br />

fi<br />

# =========================================================================<br />

LUNG_MAX=70 # Dimensione massima delle righe.<br />

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

# Una variabile può contenere uno script sed.<br />

scriptsed=’s/^>//<br />

s/^ *>//<br />

s/^ *//<br />

s/ *//’<br />

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

# Cancella gli accenti circonflessi e le tabulazioni presenti<br />

#+ all’inizio delle righe, quin<strong>di</strong> <strong>di</strong>mensiona le righe a $LUNG_MAX caratteri.<br />

sed "$scriptsed" $1 | fold -s --width=$LUNG_MAX<br />

# -s opzione <strong>di</strong> "fold" per sud<strong>di</strong>videre le righe<br />

#+ in corrispondenza <strong>di</strong> uno spazio, se possibile.<br />

611

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

Saved successfully!

Ooh no, something went wrong!