Shell script to exit the program whenever a key is pressed

February 16, 2012 at 00:53:16
Specs: CentOS
I'm running a few operations using shell scripting. I'm looking for a piece of code that will do the following.

Whenever the key x is pressed, the script should be terminated, and whenever the key m is pressed, it should go back to the main menu. (There is a function to invoke the main menu, so whenever m is pressed, it should call that function).

These keys x and m should work at all times, ie even if I'm halfway thru the execution, if i press the key x, the script should immediately be terminated. Is there any possible way to implement this?

P.S. : It would be better if a key combination is used, Eg. ctrl+x for exit and ctrl+m for main menu

Please help


See More: Shell script to exit the program whenever a key is pressed

Report •

#1
February 16, 2012 at 21:15:57
Are you aware that in a shell script that the hardware interrupt will terminate a shell script? In most *nix systems, the default interrupt is control-c. The stty command can change it:

stty intr '^?' # change the interrupt to the delete key
stty intr '^c' # change it back to control-c

Be advised that the interrupt can be disabled in a shell script by using the trap command. If the interrupt is control-c, the following command just prints the string 'interrupt trapped' if the control-c is pressed:

trap "echo 'interrupt trapped" INT

Now, for the second part of your question: what does "whenever the key m is pressed, it should go back to the main menu" mean? Does it mean that your main men is within the script and if a key combination like control-m is pressed, return to the main menu?

If that's what you mean, then you have to program for that somewhere in the code. Also, reading special characters in the shell is not very easy. This article discusses it:

http://collaboration.cmc.ec.gc.ca/s...



Report •

#2
February 21, 2012 at 23:20:00
Nails,

I got the ctrl + c part to terminate the code, but i think i wasn't clear enough with the second part of my question.

My main menu is within a function in the script, and i need a code that would behave just like the ctrl + c interruppt, in the sense that just like how the script is terminated whenever ctrl+c is pressed, this code should evoke the main menu function whenever that key or key combination is pressed, even if the script is halfway through execution.

How do i do this?


Report •

#3
March 17, 2012 at 23:43:50
Sanjeev:

I realize this is a little old, but I thought you might find it interesting that you can put a function within a script into background. That might be the path you would need to take in order to accomplish what you and nails discussed. Here's an example of a script that will run until you hit any key (and I mean any key).

#!/bin/ksh

function print_something {

while true
do
print "Hi mom\n"
sleep 2
done

}

function hitAnyKey {

trap `print Ouch!!!!` 1 2 3 15
PATH="/bin:/usr/bin:/usr/sbin"
stty raw
print "\n\n\tHit any key to terminate"
InKey=$(dd bs=5 count=1 2>/dev/null)
stty -raw

}

print_something &
PID=$!
hitAnyKey
kill -9 $PID

In your case, the function that would trap the desired keys would be run in the foreground while your script ran in the background. When the appropriate key was hit, the foreground script would send a kill to your background process and then simply run your main script. The trap is in there simply to demonstrate how to trap a keyboard interrupt. You don't need to include it.

i wrote this script years ago when I was teaching some newbies some scripting tricks.


Report •

Related Solutions

#4
March 26, 2012 at 00:22:29
Thepubba1,

Report •

#5
March 26, 2012 at 00:24:41
Thepubba1,

This is the logic i've been looking for, but i'm unable to implement your code since I'm using bash and not ksh.

Anyway, i've developed it to work in bash.

Thanks for your help...!


Report •

#6
August 4, 2012 at 04:40:19
There's a command in Linux which allows to read input but also to time-out, and I forgot the name ;)

But I can look it up

This is a way to create a loop, and the rest of the time, you can run some other code ... but you could not interrupt while that code is running. Or, you launch that part seperately, but then things become complex.


Report •

Ask Question