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.

}<br />

Capitolo 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

# Scrive il nome della <strong>di</strong>rectory come prima riga <strong>di</strong> dbfile.<br />

md5sum "$<strong>di</strong>rectory"/* >> "$dbfile"<br />

# Accoda le checksum md5 e i nomi dei file.<br />

verifica_database ()<br />

{<br />

local n=0<br />

local nomefile<br />

local checksum<br />

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

# Questa verifica potrebbe anche non essere necessaria, ma è<br />

#+ meglio essere pignoli che rischiare.<br />

if [ ! -r "$dbfile" ]<br />

then<br />

echo "Impossibile leggere il database delle checksum!"<br />

exit $E_ERR_DBFILE<br />

fi<br />

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

while read record[n]<br />

do<br />

<strong>di</strong>rectory_verificata="${record[0]}"<br />

if [ "$<strong>di</strong>rectory_verificata" != "$<strong>di</strong>rectory" ]<br />

then<br />

echo "Le <strong>di</strong>rectory non corrispondono!"<br />

# E’ stato in<strong>di</strong>cato un nome <strong>di</strong> <strong>di</strong>rectory sbagliato.<br />

exit $E_DIR_ERRATA<br />

fi<br />

if [ "$n" -gt 0 ] # Non è il nome della <strong>di</strong>rectory.<br />

then<br />

nomefile[n]=$( echo ${record[$n]} | awk ’{ print $2 }’ )<br />

# md5sum scrive nel primo campo la checksum, nel<br />

#+ secondo il nome del file.<br />

checksum[n]=$( md5sum "${nomefile[n]}" )<br />

if [ "${record[n]}" = "${checksum[n]}" ]<br />

then<br />

echo "${nomefile[n]} non è stato mo<strong>di</strong>ficato."<br />

elif [ "‘basename ${nomefile[n]}‘" != "$dbfile" ]<br />

# Salta il database delle checksum,<br />

#+ perché cambia ad ogni invocazione dello script.<br />

# ---<br />

# Questo significa, purtroppo, che quando si esegue<br />

#+ lo script su $PWD, la coincidenza con il<br />

#+ file database delle checksum non viene rilevata.<br />

# Esercizio: Risolvete questo problema.<br />

290

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

Saved successfully!

Ooh no, something went wrong!