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.

Ve<strong>di</strong> Esempio 34-4 per una <strong>di</strong>mostrazione dei costrutti con<strong>di</strong>zionali if/then annidati.<br />

7.5. Test sulla conoscenza delle verifiche<br />

Note<br />

Capitolo 7. Verifiche<br />

Il file <strong>di</strong> sistema xinitrc viene <strong>di</strong> solito impiegato, tra l’altro, per mettere in esecuzione il server X.<br />

Questo file contiene un certo numero <strong>di</strong> costrutti if/then, come mostra il seguente frammento.<br />

if [ -f $HOME/.Xclients ]; then<br />

exec $HOME/.Xclients<br />

elif [ -f /etc/X11/xinit/Xclients ]; then<br />

exec /etc/X11/xinit/Xclients<br />

else<br />

# failsafe settings. Although we should never get here<br />

# (we provide fallbacks in Xclients as well) it can’t hurt.<br />

xclock -geometry 100x100-5+5 &<br />

xterm -geometry 80x50-50+150 &<br />

if [ -f /usr/bin/netscape -a -f /usr/share/doc/HTML/index.html ]; then<br />

netscape /usr/share/doc/HTML/index.html &<br />

fi<br />

fi<br />

Spiegate i costrutti <strong>di</strong> “verifica” del frammento precedente, quin<strong>di</strong> esaminate l’intero file<br />

/etc/X11/xinit/xinitrc ed analizzate i costrutti if/then. È necessario consultare i capitoli<br />

riguardanti grep, sed e le espressioni regolari più avanti.<br />

1. Fate attenzione che il bit suid impostato su file binari (eseguibili) può aprire falle <strong>di</strong> sicurezza. Il bit<br />

suid non ha alcun effetto sugli script <strong>di</strong> shell.<br />

2. Nei moderni sistemi UNIX, lo sticky bit viene utilizzato solo sulle <strong>di</strong>rectory e non più sui file.<br />

3. Come sottolinea S.C., in una verifica composta, il quoting <strong>di</strong> una variabile stringa può non essere<br />

sufficiente. [ -n "$stringa" -o "$a" = "$b" ] potrebbe, con alcune versioni <strong>di</strong> <strong>Bash</strong>,<br />

provocare un errore se $stringa fosse vuota. Il modo per evitarlo è quello <strong>di</strong> aggiungere un<br />

carattere extra alle variabili che potrebbero essere vuote, [ "x$stringa" != x -o "x$a" =<br />

"x$b" ] (le “x” si annullano).<br />

82

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

Saved successfully!

Ooh no, something went wrong!