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 bc non è installato.<br />

echo "Esecuzione interrotta . . . "<br />

exit $E_NOESEC<br />

fi<br />

if ! which md5sum &> /dev/null; then<br />

echo md5sum non è installato.<br />

echo "Esecuzione interrotta . . . "<br />

exit $E_NOESEC<br />

fi<br />

if ! which usleep &> /dev/null; then<br />

echo usleep non è installato.<br />

echo "Esecuzione interrotta . . . "<br />

exit $E_NOESEC<br />

fi<br />

Capitolo 33. Miscellanea<br />

# Impostate la variabile seguente per rallentare l’esecuzione dello script.<br />

# Viene passata come argomento a usleep (man usleep)<br />

#+ e viene espressa in microsecon<strong>di</strong> (500000 = mezzo secondo).<br />

USLEEP_ARG=0<br />

# Cancellazione della <strong>di</strong>rectory temporanea, ripristino del cursore e<br />

#+ dei colori del terminale -- nel caso lo script venga interrotto con Ctl-C.<br />

trap ’echo -en "\E[?25h"; echo -en "\E[0m"; stty echo;\<br />

tput cup 20 0; rm -fr $CORSA_CAVALLI_DIR_TMP’ TERM EXIT<br />

# Ve<strong>di</strong> il capitolo sul debugging per la spiegazione <strong>di</strong> ’trap.’<br />

# Impostazione <strong>di</strong> un nome univoco (paranoico) per la <strong>di</strong>rectory temporanea<br />

#+ necessaria allo script.<br />

CORSA_CAVALLI_DIR_TMP=$HOME/.corsacavalli-‘date +%s‘-‘head -c10 /dev/urandom |\<br />

md5sum | head -c30‘<br />

# Crea la <strong>di</strong>rectory temporanea e vi accede.<br />

mk<strong>di</strong>r $CORSA_CAVALLI_DIR_TMP<br />

cd $CORSA_CAVALLI_DIR_TMP<br />

# La funzione che segue serve a spostare il cursore alla riga $1 colonna $2,<br />

#+ e a visualizzare $3.<br />

# Es.: "sposta_e_visualizza 5 10 linux" equivale a<br />

#+ "tput cup 4 9; echo linux", con un unico comando, però, invece <strong>di</strong> due.<br />

# Nota: "tput cup" identifica con 0 0 l’angolo superiore sinistro del<br />

#+ terminale, mentre echo lo identifica con 1 1.<br />

sposta_e_visualizza() {<br />

echo -ne "\E[${1};${2}H""$3"<br />

}<br />

# Funzione per generare un numero pseudocasuale compreso tra 1 e 9.<br />

casuale_1_9 () {<br />

head -c10 /dev/urandom | md5sum | tr -d [a-z] | tr -d 0 | cut -c1<br />

}<br />

# Due funzioni per simulare il "movimento" dei cavalli.<br />

<strong>di</strong>segna_cavallo_uno() {<br />

564

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

Saved successfully!

Ooh no, something went wrong!