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 31. Precauzioni<br />

echo; echo<br />

echo "uno due tre" | ( read a b c;<br />

echo "nella subshell: "; echo "a = $a"; echo "b = $b"; echo "c = $c" )<br />

# a = uno<br />

# b = due<br />

# c = tre<br />

echo "----------------------"<br />

echo "Fuori dalla subshell: "<br />

echo "a = $a" # a = aaa<br />

echo "b = $b" # b = bbb<br />

echo "c = $c" # c = ccc<br />

echo<br />

exit 0<br />

In effetti, come fa notare Anthony Richardson, usare la pipe con qualsiasi ciclo può provocare un simile<br />

problema.<br />

# Problemi nell’uso <strong>di</strong> una pipe con un ciclo.<br />

# Esempio <strong>di</strong> Anthony Richardson.<br />

#+ con appen<strong>di</strong>ce <strong>di</strong> Wilbert Berendsen.<br />

trovato=falso<br />

find $HOME -type f -atime +30 -size 100k |<br />

while true<br />

do<br />

read f<br />

echo "$f supera i 100KB e non è stato usato da più <strong>di</strong> 30 giorni"<br />

echo "Considerate la possibilità <strong>di</strong> spostarlo in un archivio."<br />

trovato=vero<br />

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

"Livello della subshell = $BASH_SUBSHELL"<br />

# Livello della subshell = 1<br />

# Si, siete all’interno <strong>di</strong> una subshell.<br />

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

# In questo caso trovato sarà sempre falso perché<br />

#+ è stato impostato all’interno <strong>di</strong> una subshell<br />

if [ $trovato = falso ]<br />

then<br />

echo "Nessun file da archiviare."<br />

fi<br />

# =====================Ora nel modo corretto:========================<br />

trovato=falso<br />

for f in $(find $HOME -type f -atime +30 -size 100k) # Nessuna pipe.<br />

538

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

Saved successfully!

Ooh no, something went wrong!