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.

Capitolo 10. Cicli ed alternative<br />

##-------due tab---- servono a concatenare gli zeri al parametro $2<br />

done<br />

# (Grazie, S.C., per i chiarimenti aggiuntivi.)<br />

exit 0<br />

In un ciclo for, una variabile può sostituire [lista].<br />

Esempio 10-3. Fileinfo: operare su un elenco <strong>di</strong> file contenuto in una variabile<br />

#!/bin/bash<br />

# fileinfo.sh<br />

FILE="/usr/sbin/accept<br />

/usr/sbin/pwck<br />

/usr/sbin/chroot<br />

/usr/bin/fakefile<br />

/sbin/badblocks<br />

/sbin/ypbind" # Elenco dei file sui quali volete informazioni.<br />

# Compreso l’inesistente file /usr/bin/fakefile.<br />

echo<br />

for file in $FILE<br />

do<br />

if [ ! -e "$file" ] # Verifica se il file esiste.<br />

then<br />

echo "$file non esiste."; echo<br />

continue # Verifica il successivo.<br />

fi<br />

ls -l $file | awk ’{ print $9 " <strong>di</strong>mensione file: " $5 }’<br />

# Visualizza 2 campi.<br />

whatis ‘basename $file‘ # Informazioni sul file.<br />

# Fate attenzione che, affinché questo script funzioni correttamente,<br />

#+ bisogna aver impostato il database whatis.<br />

# Per farlo, da root, eseguite /usr/bin/makewhatis.<br />

echo<br />

done<br />

exit 0<br />

In un ciclo for è possibile il globbing se in [lista] sono presenti i caratteri jolly (* e ?), che<br />

vengono usati per l’espansione dei nomi dei file.<br />

161

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

Saved successfully!

Ooh no, something went wrong!