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.

exit 0<br />

Capitolo 20. Subshell<br />

Una subshell può essere usata per impostare un “ambiente de<strong>di</strong>cato” per un gruppo <strong>di</strong> coman<strong>di</strong>.<br />

COMANDO1<br />

COMANDO2<br />

COMANDO3<br />

(<br />

IFS=:<br />

PATH=/bin<br />

unset TERMINFO<br />

set -C<br />

shift 5<br />

COMANDO4<br />

COMANDO5<br />

exit 3 # Esce solo dalla subshell.<br />

)<br />

# La shell genitore non è stata toccata ed il suo ambiente è preservato.<br />

COMANDO6<br />

COMANDO7<br />

Una sua applicazione permette <strong>di</strong> verificare se una variabile è stata definita.<br />

if (set -u; : $variabile) 2> /dev/null<br />

then<br />

echo "La variabile è impostata."<br />

fi # La variabile potrebbe essere stata impostata nello script stesso,<br />

#+ oppure essere una variabile interna <strong>di</strong> <strong>Bash</strong>,<br />

#+ oppure trattarsi <strong>di</strong> una variabile d’ambiente (che è stata esportata).<br />

# Si sarebbe anche potuto scrivere<br />

# [[ ${variabile-x} != x || ${variabile-y} !=y ]]<br />

# oppure [[ ${variabile-x} != x$variabile ]]<br />

# oppure [[ ${variabile+x} = x ]]<br />

# oppure [[ ${variabile-x} != x ]]<br />

Un’altra applicazione è quella <strong>di</strong> verificare la presenza <strong>di</strong> un file lock:<br />

if (set -C; : > file_lock) 2> /dev/null<br />

then<br />

: # il file_lock non esiste: nessun utente sta eseguendo lo script<br />

else<br />

echo "C’è già un altro utente che sta eseguendo quello script."<br />

exit 65<br />

fi<br />

# Frammento <strong>di</strong> co<strong>di</strong>ce <strong>di</strong> Stéphane Chazelas,<br />

#+ con mo<strong>di</strong>fiche effettuate da Paulo Marcel Coelho Aragao.<br />

436

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

Saved successfully!

Ooh no, something went wrong!