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.

echo "Questo richiede un po’ <strong>di</strong> tempo."<br />

updatedb /usr & # Deve essere eseguito come root.<br />

Capitolo 11. Coman<strong>di</strong> interni e builtin<br />

wait<br />

# Non viene eseguita la parte restante dello script finché ’updatedb’ non<br />

#+ ha terminato il proprio compito.<br />

# Si vuole che il database sia aggiornato prima <strong>di</strong> cercare un nome <strong>di</strong> file.<br />

locate $1<br />

# Senza il comando wait, nell’ipotesi peggiore, lo script sarebbe uscito<br />

#+ mentre ’updatedb’ era ancora in esecuzione, trasformandolo in un processo<br />

#+ orfano.<br />

exit 0<br />

Opzionalmente, wait può avere come argomento un identificatore <strong>di</strong> job, per esempio, wait%1 o<br />

wait $PPID. Ve<strong>di</strong> la tabella degli identificatori <strong>di</strong> job.<br />

Suggerimento: In uno script, far eseguire un comando in background, per mezzo della E<br />

commerciale (&), può causare la sospensione dello script finché non viene premuto il tasto<br />

INVIO. Questo sembra capitare con i coman<strong>di</strong> che scrivono allo stdout. Può rappresentare un<br />

grande fasti<strong>di</strong>o.<br />

#!/bin/bash<br />

# test.sh<br />

ls -l &<br />

echo "Fatto."<br />

bash$ ./test.sh<br />

Fatto.<br />

[bozo@localhost test-scripts]$ total 1<br />

-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh<br />

_<br />

Mettendo wait dopo il comando che deve essere eseguito in background si rime<strong>di</strong>a a questo<br />

comportamento.<br />

#!/bin/bash<br />

# test.sh<br />

ls -l &<br />

echo "Fatto."<br />

wait<br />

bash$ ./test.sh<br />

Fatto.<br />

[bozo@localhost test-scripts]$ total 1<br />

-rwxr-xr-x 1 bozo bozo 34 Oct 11 15:09 test.sh<br />

220

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

Saved successfully!

Ooh no, something went wrong!