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 12. Filtri, programmi e coman<strong>di</strong> esterni<br />

find "$DIR" -type f -atime +5 -exec rm {} \;<br />

# ^^<br />

# Le parentesi graffe rappresentano il percorso completo prodotto da "find."<br />

#<br />

# Cancella tutti il file in "/home/bozo/junk_files"<br />

#+ a cui non si è acceduto da almeno 5 giorni.<br />

#<br />

# "-type tipofile", dove<br />

# f = file regolare<br />

# d = <strong>di</strong>rectory, ecc.<br />

# (La pagina <strong>di</strong> manuale <strong>di</strong> ’find’ contiene l’elenco completo.)<br />

find /etc -exec grep ’[0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*[.][0-9][0-9]*’ {} \;<br />

# Trova tutti gli in<strong>di</strong>rizzi IP (xxx.xxx.xxx.xxx) nei file della <strong>di</strong>rectory /etc.<br />

# Ci sono alcuni caratteri non essenziali. Come possono essere rimossi?<br />

# Ecco una possibilità:<br />

find /etc -type f -exec cat ’{}’ \; | tr -c ’.[:<strong>di</strong>git:]’ ’\n’ \<br />

| grep ’^[^.][^.]*\.[^.][^.]*\.[^.][^.]*\.[^.][^.]*$’<br />

#<br />

# [:<strong>di</strong>git:] è una delle classi <strong>di</strong> caratteri<br />

#+ introdotta con lo standard POSIX 1003.2.<br />

# Grazie, Stéphane Chazelas.<br />

Nota: L’opzione -exec <strong>di</strong> find non deve essere confusa con il builtin <strong>di</strong> shell exec.<br />

Esempio 12-3. Badname, elimina, nella <strong>di</strong>rectory corrente, i file i cui nomi contengono<br />

caratteri inappropriati e spazi.<br />

#!/bin/bash<br />

# badname.sh<br />

# Cancella i file nella <strong>di</strong>rectory corrente contenenti caratteri inadatti.<br />

for nomefile in *<br />

do<br />

nomestrano=‘echo "$nomefile" | sed -n /[\+\{\;\"\\\=\?~\(\)\\&\*\|\$]/p‘<br />

# Anche in questo modo:<br />

# nomestrano=‘echo "$nomefile" | sed -n ’/[+{;"\=?~()&*|$]/p’‘<br />

# Cancella i file contenenti questi caratteri: + { ; " \ = ? ~ ( ) < > & * | $<br />

#<br />

rm $nomestrano 2>/dev/null<br />

# ^^^^^^^^^^^ Vengono eliminati i messaggi d’errore.<br />

done<br />

232

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

Saved successfully!

Ooh no, something went wrong!