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.

# Questo può provocare dei problemi. Ad esempio:<br />

minore=5<br />

maggiore=105<br />

if [ "$maggiore" \< "$minore" ]<br />

then<br />

echo "$maggiore è minore <strong>di</strong> $minore"<br />

fi # 105 è minore <strong>di</strong> 5<br />

# Infatti, "105" è veramente minore <strong>di</strong> "5"<br />

#+ in un confronto <strong>di</strong> stringhe (or<strong>di</strong>ne ASCII).<br />

echo<br />

exit 0<br />

Capitolo 31. Precauzioni<br />

Talvolta è necessario il quoting (apici doppi) per le variabili che si trovano all’interno del costrutto <strong>di</strong><br />

“verifica” parentesi quadre ([]). Non farne uso può causare un comportamento inaspettato. Ve<strong>di</strong> Esempio<br />

7-6, Esempio 16-5 e Esempio 9-6.<br />

Coman<strong>di</strong> inseriti in uno script possono fallire l’esecuzione se il proprietario dello script non ha, per quei<br />

coman<strong>di</strong>, i permessi d’esecuzione. Se un utente non può invocare un comando al prompt <strong>di</strong> shell, il fatto<br />

<strong>di</strong> inserirlo in uno script non cambia la situazione. Si provi a cambiare gli attributi dei coman<strong>di</strong> in<br />

questione, magari impostando il bit suid (come root, naturalmente).<br />

Cercare <strong>di</strong> usare il - come operatore <strong>di</strong> re<strong>di</strong>rezione (che non è) <strong>di</strong> solito provoca spiacevoli sorprese.<br />

comando1 2> - | comando2 # Il tentativo <strong>di</strong> re<strong>di</strong>rigere l’output<br />

#+ d’errore <strong>di</strong> comando1 con una pipe...<br />

# ...non funziona.<br />

comando1 2>& - | comando2 # Altrettanto inutile.<br />

Grazie, S.C.<br />

Usare le funzionalità <strong>di</strong> <strong>Bash</strong> versione 2+ può provocare l’uscita dal programma con un messaggio<br />

d’errore. Le macchine Linux più datate potrebbero avere, come installazione predefinita, la versione<br />

<strong>Bash</strong> 1.XX.<br />

#!/bin/bash<br />

versione_minima=2<br />

# Dal momento che Chet Ramey sta costantemente aggiungendo funzionalità a <strong>Bash</strong>,<br />

# si può impostare $versione_minima a 2.XX, o ad altro valore appropriato.<br />

E_ERR_VERSIONE=80<br />

534

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

Saved successfully!

Ooh no, something went wrong!