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.

)<br />

Capitolo 20. Subshell<br />

echo<br />

echo "Livello della subshell all’ESTERNO della subshell = $BASH_SUBSHELL"<br />

echo<br />

if [ -z "$variabile_interna" ]<br />

then<br />

echo "variabile_interna non definita nel corpo principale della shell"<br />

else<br />

echo "variabile_interna definita nel corpo principale della shell"<br />

fi<br />

echo "Nel corpo principale della shell,\<br />

\"variabile_interna\" = $variabile_interna"<br />

# $variabile_interna viene in<strong>di</strong>cata come non inizializzata perché<br />

#+ le variabili definite in una subshell sono "variabili locali".<br />

# Esiste un rime<strong>di</strong>o a ciò?<br />

echo<br />

exit 0<br />

Ve<strong>di</strong> anche Esempio 31-2.<br />

+<br />

I cambiamenti <strong>di</strong> <strong>di</strong>rectory effettuati in una subshell non si ripercuotono sulla shell genitore.<br />

Esempio 20-2. Elenco dei profili utente<br />

#!/bin/bash<br />

# allprofs.sh: visualizza i profili <strong>di</strong> tutti gli utenti<br />

# Script <strong>di</strong> Heiner Steven mo<strong>di</strong>ficato dall’autore <strong>di</strong> questo documento.<br />

FILE=.bashrc # Il file contenente il profilo utente<br />

#+ nello script originale era ".profile".<br />

for home in ‘awk -F: ’{print $6}’ /etc/passwd‘<br />

do<br />

[ -d "$home" ] || continue # Se non vi è la <strong>di</strong>rectory home,<br />

#+ va al successivo.<br />

[ -r "$home" ] || continue # Se non ha i permessi <strong>di</strong> lettura, va<br />

#+ al successivo.<br />

(cd $home; [ -e $FILE ] && less $FILE)<br />

done<br />

# Quando lo script termina, non è necessario un ’cd’ alla <strong>di</strong>rectory<br />

#+ originaria, perché ’cd $home’ è stato eseguito in una subshell.<br />

435

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

Saved successfully!

Ooh no, something went wrong!