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.

echo<br />

a=39<br />

b=36<br />

exit 0<br />

# Notate che anche se si commenta il comando ’exit’ questo non fa<br />

#+ alcuna <strong>di</strong>fferenza, poiché lo script esce in ogni caso dopo<br />

#+ l’esecuzione dei coman<strong>di</strong>.<br />

Esempio 29-6. Pulizia dopo un Control-C<br />

#!/bin/bash<br />

# logon.sh: Un rapido e ru<strong>di</strong>mentale script per verificare se si<br />

#+ è ancora collegati.<br />

Capitolo 29. Debugging<br />

umask 177 # Per essere certi che i file temporanei non siano leggibili dal<br />

#+ mondo intero.<br />

TRUE=1<br />

FILELOG=/var/log/messages<br />

# Fate attenzione che $FILELOG deve avere i permessi <strong>di</strong> lettura<br />

#+ (da root, chmod 644 /var/log/messages).<br />

FILETEMP=temp.$$<br />

# Crea un file temporaneo con un nome "univoco", usando l’id <strong>di</strong><br />

#+ processo dello script.<br />

# Un’alternativa è usare ’mktemp’.<br />

# Per esempio:<br />

# FILETEMP=‘mktemp temp.XXXXXX‘<br />

PAROLACHIAVE=address<br />

# A collegamento avvenuto, la riga "remote IP address xxx.xxx.xxx.xxx"<br />

# viene accodata in /var/log/messages.<br />

COLLEGATO=22<br />

INTERRUPT_UTENTE=13<br />

CONTROLLA_RIGHE=100<br />

# Numero <strong>di</strong> righe del file <strong>di</strong> log da controllare.<br />

trap ’rm -f $FILETEMP; exit $INTERRUPT_UTENTE’; TERM INT<br />

# Cancella il file temporaneo se lo script viene interrotto con un control-c.<br />

echo<br />

while [ $TRUE ] # Ciclo infinito.<br />

do<br />

tail -$CONTROLLA_RIGHE $FILELOG> $FILETEMP<br />

# Salva le ultime 100 righe del file <strong>di</strong> log <strong>di</strong> sistema nel file<br />

#+ temporaneo. Necessario, dal momento che i kernel più<br />

#+ recenti generano molti messaggi <strong>di</strong> log durante la fase <strong>di</strong> avvio.<br />

ricerca=‘grep $PAROLACHIAVE $FILETEMP‘<br />

# Verifica la presenza della frase "IP address",<br />

520

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

Saved successfully!

Ooh no, something went wrong!