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.

echo<br />

exit 0<br />

Capitolo 11. Coman<strong>di</strong> interni e builtin<br />

Il comando read possiede alcune interessanti opzioni che consentono <strong>di</strong> visualizzare un prompt e<br />

persino <strong>di</strong> leggere i tasti premuti senza il bisogno <strong>di</strong> premere INVIO.<br />

# Rilevare la pressione <strong>di</strong> un tasto senza dover premere INVIO.<br />

read -s -n1 -p "Premi un tasto " tasto<br />

echo; echo "Hai premuto il tasto "\"$tasto\""."<br />

# L’opzione -s serve a non visualizzare l’input.<br />

# L’opzione -n N in<strong>di</strong>ca che devono essere accettati solo N caratteri <strong>di</strong> input.<br />

# L’opzione -p permette <strong>di</strong> visualizzare il messaggio del prompt imme<strong>di</strong>atamente<br />

#+ successivo, prima <strong>di</strong> leggere l’input.<br />

# Usare queste opzioni è un po’ complicato, perché<br />

#+ devono essere poste nell’or<strong>di</strong>ne esatto.<br />

L’opzione -n <strong>di</strong> read consente anche il rilevamento dei tasti freccia ed alcuni altri tasti inusuali.<br />

Esempio 11-6. Rilevare i tasti freccia<br />

#!/bin/bash<br />

# arrow-detect.sh: Rileva i tasti freccia, e qualcos’altro.<br />

# Grazie a Sandro Magi per avermelo mostrato.<br />

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

# Co<strong>di</strong>ce dei caratteri generati dalla pressione dei tasti.<br />

frecciasu=’\[A’<br />

frecciagiù=’\[B’<br />

frecciadestra=’\[C’<br />

frecciasinistra=’\[D’<br />

ins=’\[2’<br />

canc=’\[3’<br />

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

SUCCESSO=0<br />

ALTRO=65<br />

echo -n "Premi un tasto... "<br />

# Potrebbe essere necessario premere anche INVIO se viene premuto un<br />

#+ tasto non tra quelli elencati.<br />

read -n3 tasto # Legge 3 caratteri.<br />

echo -n "$tasto" | grep "$frecciasu" # Verifica il co<strong>di</strong>ce del<br />

#+ tasto premuto.<br />

195

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

Saved successfully!

Ooh no, something went wrong!