Manuel
Manuel

Reputation: 97

How to detect when user press ESC and do something in shell script

I want to create a poll in a shell script:

echo "1 - What is your name?"
read name
echo "your name is $name"

echo "2 - What is your country?"
read country
echo "your country is $country"
...
...etc...
...

I want if the user press ESC in a question, cancel the question an jump to the next question.

Thanks! I keep in wait for possibles answers!

Upvotes: 8

Views: 10163

Answers (2)

fig
fig

Reputation: 813

If you end up using bash shell, you might be interested in the "trap" command, which allows you to trap and act on interupts (e.g. ctrl-C)

E.g: http://kb.mit.edu/confluence/pages/viewpage.action?pageId=3907156

Upvotes: 1

Nimrod007
Nimrod007

Reputation: 9913

this is how to know if user selected escape :

read -s -n1  key

 case $key in
     $'\e') echo "Escape";;
 esac

Upvotes: 12

Related Questions