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.

Cautela<br />

La sostituzione <strong>di</strong> comando può dar luogo alla sud<strong>di</strong>visione delle parole.<br />

COMANDO ‘echo a b‘ # 2 argomenti: a e b;<br />

COMANDO "‘echo a b‘" # 1 argomento: "a b"<br />

COMANDO ‘echo‘ # nessun argomento<br />

COMANDO "‘echo‘" # un argomento vuoto<br />

# Grazie, S.C.<br />

Anche quando la sud<strong>di</strong>visione delle parole non si verifica, la sostituzione <strong>di</strong><br />

comando rimuove i ritorni a capo finali.<br />

Capitolo 14. Sostituzione <strong>di</strong> comando<br />

# cd "‘pwd‘" # Questo dovrebbe funzionare sempre.<br />

# Tuttavia...<br />

mk<strong>di</strong>r ’nome <strong>di</strong> <strong>di</strong>rectory con un carattere <strong>di</strong> a capo finale<br />

’<br />

cd ’nome <strong>di</strong> <strong>di</strong>rectory con un carattere <strong>di</strong> a capo finale<br />

’<br />

cd "‘pwd‘" # Messaggio d’errore:<br />

# bash: cd: /tmp/file with trailing newline: No such file or <strong>di</strong>rectory<br />

cd "$PWD" # Funziona bene.<br />

precedenti_impostazioni_tty=$(stty -g) # Salva le precedenti impostazioni<br />

#+ del terminale.<br />

echo "Premi un tasto "<br />

stty -icanon -echo # Disabilita la modalità<br />

#+ "canonica" del terminale.<br />

# Disabilita anche l’echo *locale*.<br />

tasto=$(dd bs=1 count=1 2> /dev/null) # Uso <strong>di</strong> ’dd’ per rilevare il<br />

#+ tasto premuto.<br />

stty "$precedenti_impostazioni_tty" # Ripristina le vecchie impostazioni.<br />

echo "Hai premuto ${#tasto} tasto/i." # ${#variabile} = numero <strong>di</strong> caratteri<br />

#+ in $variabile<br />

#<br />

# Premete qualsiasi tasto tranne INVIO, l’output sarà "Hai premuto 1 tasto/i."<br />

# Premete INVIO e sarà "Hai premuto 0 tasto/i."<br />

# Nella sostituzione <strong>di</strong> comando i ritorni a capo vengono eliminati.<br />

Grazie, S.C.<br />

384

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

Saved successfully!

Ooh no, something went wrong!