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.

Esempio 7-4. Ricerca <strong>di</strong> link interrotti (broken link)<br />

#!/bin/bash<br />

# broken-link.sh<br />

# Scritto da Lee Bigelow <br />

# Utilizzato con il consenso dell’autore.<br />

Capitolo 7. Verifiche<br />

# Uno script <strong>di</strong> pura shell per cercare i link simbolici "morti" e visualizzarli<br />

#+ tra virgolette, in modo tale che possano essere trattati e dati in pasto a<br />

#+ xargs :) es. broken-link.sh /una<strong>di</strong>rectory /altra<strong>di</strong>rectory | xargs rm<br />

#<br />

#Il seguente, tuttavia, è il metodo migliore:<br />

#<br />

#find "una<strong>di</strong>rectory" -type l -print0|\<br />

#xargs -r0 file|\<br />

#grep "broken symbolic"|<br />

#sed -e ’s/^\|: *broken symbolic.*$/"/g’<br />

#<br />

#ma non sarebbe bash pura, come deve essere.<br />

#Prudenza: state attenti al file <strong>di</strong> sistema /proc e a tutti i link circolari!<br />

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

# Se nessun argomento viene passato allo script, la <strong>di</strong>rectory <strong>di</strong> ricerca<br />

#+ <strong>di</strong>rectorys viene impostata alla <strong>di</strong>rectory corrente. Altrimenti <strong>di</strong>rectorys<br />

#+ viene impostata all’argomento passato.<br />

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

[ $# -eq 0 ] && <strong>di</strong>rectorys=‘pwd‘ || <strong>di</strong>rectorys=$@<br />

# Implementazione della funzione verlink per cercare, nella <strong>di</strong>rectory<br />

# passatale, i file che sono link a file inesistenti, quin<strong>di</strong> visualizzarli<br />

#+ tra virgolette. Se uno degli elementi della <strong>di</strong>rectory è una sotto<strong>di</strong>rectory,<br />

#+ allora anche questa viene passata alla funzione verlink.<br />

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

verlink () {<br />

for elemento in $1/*; do<br />

[ -h "$elemento" -a ! -e "$elemento" ] && echo \"$elemento\"<br />

[ -d "$elemento" ] && verlink $elemento<br />

# Naturalmente, ’-h’ verifica i link simbolici, ’-d’ le <strong>di</strong>rectory.<br />

done<br />

}<br />

# Invia ogni argomento passato allo script alla funzione verlink, se è una<br />

#+ <strong>di</strong>rectory valida. Altrimenti viene visualizzato un messaggio d’errore e le<br />

#+ informazioni sull’utilizzo.<br />

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

for <strong>di</strong>rectory in $<strong>di</strong>rectorys; do<br />

if [ -d $<strong>di</strong>rectory ]<br />

then verlink $<strong>di</strong>rectory<br />

else<br />

echo "$<strong>di</strong>rectory non è una <strong>di</strong>rectory"<br />

echo "Utilizzo: $0 <strong>di</strong>r1 <strong>di</strong>r2 ..."<br />

fi<br />

73

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

Saved successfully!

Ooh no, something went wrong!