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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

# Ora ci occupiamo dei file contenenti ogni tipo <strong>di</strong> spaziatura.<br />

find . -name "* *" -exec rm -f {} \;<br />

# Il percorso del file che "find" cerca prende il posto <strong>di</strong> "{}".<br />

# La ’\’ assicura che il ’;’ sia interpretato correttamente come fine del<br />

#+ comando.<br />

exit 0<br />

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

# I seguenti coman<strong>di</strong> non vengono eseguiti a causa dell’"exit" precedente.<br />

# Un’alternativa allo script visto prima:<br />

find . -name ’*[+{;"\\=?~()&*|$ ]*’ -exec rm -f ’{}’ \;<br />

# (Grazie, S.C.)<br />

Esempio 12-4. Cancellare un file tramite il suo numero <strong>di</strong> inode<br />

#!/bin/bash<br />

# idelete.sh: Cancellare un file per mezzo del suo numero <strong>di</strong> inode.<br />

# Questo si rivela utile quando il nome del file inizia con un<br />

#+ carattere scorretto, come ? o -.<br />

CONTA_ARG=1 # Allo script deve essere passato come argomento<br />

#+ il nome del file.<br />

E_ERR_ARG=70<br />

E_FILE_NON_ESISTE=71<br />

E_CAMBIO_IDEA=72<br />

if [ $# -ne "$CONTA_ARG" ]<br />

then<br />

echo "Utilizzo: ‘basename $0‘ nomefile"<br />

exit $E_ERR_ARG<br />

fi<br />

if [ ! -e "$1" ]<br />

then<br />

echo "Il file \""$1"\" non esiste."<br />

exit $E_FILE_NON_ESISTE<br />

fi<br />

inum=‘ls -i | grep "$1" | awk ’{print $1}’‘<br />

# inum = numero <strong>di</strong> inode (index node) del file<br />

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

# Tutti i file posseggono un inode, la registrazione che contiene<br />

#+ informazioni sull’in<strong>di</strong>rizzo fisico del file stesso.<br />

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

echo; echo -n "Sei assolutamente sicuro <strong>di</strong> voler cancellare \"$1\"(s/n)?"<br />

# Anche ’rm’ con l’opzione ’-v’ visualizza la stessa domanda.<br />

read risposta<br />

case "$risposta" in<br />

233

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

Saved successfully!

Ooh no, something went wrong!