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.

${parametro?msg_err}<br />

${parametro:?msg_err}<br />

param6=123<br />

a=${param6+xyz}<br />

echo "a = $a" # a = xyz<br />

Capitolo 9. Variabili riviste<br />

Se parametro è impostato viene usato, altrimenti visualizza un messaggio d’errore (msg_err).<br />

Le due forme sono quasi equivalenti. I : servono solo quando parametro è stato <strong>di</strong>chiarato, ma non<br />

impostato. Come sopra.<br />

Esempio 9-14. Sostituzione <strong>di</strong> parametro e messaggi d’errore<br />

#!/bin/bash<br />

# Verifica alcune delle variabili d’ambiente <strong>di</strong> sistema.<br />

# È una buona misura preventiva.<br />

# Se, per sempio, $USER, il nome dell’utente corrente, non è impostata,<br />

#+ la macchina non può riconoscervi.<br />

: ${HOSTNAME?} ${USER?} ${HOME?} ${MAIL?}<br />

echo<br />

echo "Il nome della macchina è $HOSTNAME."<br />

echo "Tu sei $USER."<br />

echo "La <strong>di</strong>rectory home è $HOME."<br />

echo "La cartella <strong>di</strong> posta INBOX si trova in $MAIL."<br />

echo<br />

echo "Se leggete questo messaggio, vuol <strong>di</strong>re che"<br />

echo "le variabili d’ambiente più importanti sono impostate."<br />

echo<br />

echo<br />

# ------------------------------------------------------<br />

# Il costrutto ${nomevariabile?} può verificare anche<br />

#+ le variabili impostate in uno script.<br />

QuestaVariabile=Valore-<strong>di</strong>-Questa-Variabile<br />

# È da notare, en passant, che le variabili stringa possono contenere<br />

#+ caratteri che non sono consentiti se usati nei loro nomi .<br />

: ${QuestaVariabile?}<br />

echo "Il valore <strong>di</strong> QuestaVariabile è $QuestaVariabile".<br />

echo<br />

echo<br />

130

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

Saved successfully!

Ooh no, something went wrong!