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.

Usi <strong>di</strong> /dev/zero<br />

Capitolo 28. Zero e Null<br />

Svuotare automaticamente un file <strong>di</strong> log (ottimo specialmente per trattare quei <strong>di</strong>sgustosi “cookie”<br />

inviati dai siti <strong>di</strong> commercio sul Web):<br />

Esempio 28-1. Evitare i cookie<br />

if [ -f ~/.netscape/cookies ] # Se esiste, lo cancella.<br />

then<br />

rm -f ~/.netscape/cookies<br />

fi<br />

ln -s /dev/null ~/.netscape/cookies<br />

# Tutti i cookie vengono ora spe<strong>di</strong>ti nel buco nero, invece <strong>di</strong> essere salvati<br />

#+ su <strong>di</strong>sco.<br />

Come /dev/null, anche /dev/zero è uno pseudo file, ma in realtà genera un flusso <strong>di</strong> null (zeri<br />

binari, non del genere ASCII). Un output scritto nel file scompare, ed è abbastanza <strong>di</strong>fficile leggere i<br />

null reali contenuti in /dev/zero, sebbene questo possa essere fatto con od o con un e<strong>di</strong>tor<br />

esadecimale. L’uso principale <strong>di</strong> /dev/zero è quello <strong>di</strong> creare un file fittizio inizializzato, della<br />

<strong>di</strong>mensione specificata, da usare come file <strong>di</strong> scambio (swap) temporaneo.<br />

Esempio 28-2. Impostare un file <strong>di</strong> swap usando /dev/zero<br />

#!/bin/bash<br />

# Creare un file <strong>di</strong> swap.<br />

UID_ROOT=0 # Root ha $UID 0.<br />

E_ERR_UTENTE=65 # Non root?<br />

FILE=/swap<br />

DIMENSIONEBLOCCO=1024<br />

BLOCCHIMIN=40<br />

SUCCESSO=0<br />

# Questo script deve essere eseguito da root.<br />

if [ "$UID" -ne "$UID_ROOT" ]<br />

then<br />

echo; echo "Devi essere root per eseguire questo script."; echo<br />

exit $E_ERR_UTENTE<br />

fi<br />

blocchi=${1:-$BLOCCHIMIN} # Imposta a 40 blocchi il valore predefinito, se<br />

#+ non viene specificato <strong>di</strong>versamente da riga <strong>di</strong><br />

#+ comando.<br />

# Equivale al seguente blocco <strong>di</strong> co<strong>di</strong>ce.<br />

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

# if [ -n "$1" ]<br />

# then<br />

512

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

Saved successfully!

Ooh no, something went wrong!