Computing Staff
  • 0

Shell Script To Exit The Program Whenever A Key Is Pressed

  • 0

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

Share

3 Answers

  1. 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…

    • 0
  2. 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?

    • 0
  3. 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.

    • 0