Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

shell

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!

like image 416
Manuel Avatar asked Jul 14 '13 08:07

Manuel


People also ask

What is $_ in shell script?

$_ (dollar underscore) is another special bash parameter and used to reference the absolute file name of the shell or bash script which is being executed as specified in the argument list. This bash parameter is also used to hold the name of mail file while checking emails. $@

What does $$ do in shell?

The $$ variable is the PID (Process IDentifier) of the currently running shell. This can be useful for creating temporary files, such as /tmp/my-script. $$ which is useful if many instances of the script could be run at the same time, and they all need their own temporary files.

How do I know my shell interpreter?

ps -p $$ – Display your current shell name reliably. echo "$SHELL" – Print the shell for the current user but not necessarily the shell that is running at the movement. echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.

How do you Ctrl D in shell script?

In the Linux command-line shell, pressing Ctrl + D logs out of the interface. If you used the sudo command to execute commands as another user, pressing Ctrl + D exits out of that other user and puts you back as the user you originally logged into.


1 Answers

this is how to know if user selected escape :

read -s -n1  key

 case $key in
     $'\e') echo "Escape";;
 esac
like image 153
Nimrod007 Avatar answered Sep 30 '22 02:09

Nimrod007