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.

xargs<br />

[nN]) echo "Hai cambiato idea, vero?"<br />

exit $E_CAMBIO_IDEA<br />

;;<br />

*) echo "Cancello il file \"$1\".";;<br />

esac<br />

find . -inum $inum -exec rm {} \;<br />

# ^^<br />

# Le parentesi graffe sono il segnaposto<br />

#+ per il testo prodotto da "find."<br />

echo "Il file "\"$1"\" è stato cancellato!"<br />

exit 0<br />

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

Ve<strong>di</strong> Esempio 12-27, Esempio 3-4 ed Esempio 10-9 per script che utilizzano find. La relativa<br />

pagina <strong>di</strong> manuale fornisce tutti i dettagli <strong>di</strong> questo potente e complesso comando.<br />

Un filtro per fornire argomenti ad un comando ed anche uno strumento per assemblare coman<strong>di</strong>.<br />

Sud<strong>di</strong>vide il flusso <strong>di</strong> dati in parti sufficientemente piccole per essere elaborate da filtri o coman<strong>di</strong>.<br />

Lo si consideri un potente sostituto degli apici inversi. In situazioni in cui la sostituzione <strong>di</strong><br />

comando potrebbe fallire con il messaggio d’errore too many arguments sostituendola con xargs,<br />

spesso, il problema si risolve. 2 Normalmente xargs legge dallo st<strong>di</strong>n o da una pipe, ma anche<br />

dall’output <strong>di</strong> un file.<br />

Il comando predefinito per xargs è echo. Questo significa che l’input collegato a xargs perde i<br />

ritorni a capo o qualsiasi altro carattere <strong>di</strong> spaziatura.<br />

bash$ ls -l<br />

total 0<br />

-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1<br />

-rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file2<br />

bash$ ls -l | xargs<br />

total 0 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file1 -rw-rw-r-- 1 bozo bozo 0 Jan 29 23:58 file2<br />

bash$ find ~/mail -type f | xargs grep "Linux"<br />

./misc:User-Agent: slrn/0.9.8.1 (Linux)<br />

./sent-mail-jul-2005: hosted by the Linux Documentation Project.<br />

./sent-mail-jul-2005: (Linux Documentation Project Site, rtf version)<br />

./sent-mail-jul-2005: Subject: Criticism of Bozo’s Windows/Linux article<br />

./sent-mail-jul-2005: while mentioning that the Linux ext2/ext3 filesystem<br />

. . .<br />

234

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

Saved successfully!

Ooh no, something went wrong!