Create interrupt in bash by keys like ENTER Or ESC

I need to know is it possible to interrupt a bash script using keys like ESC or ENTER ? By sending SIGINT /CTRL + C I am able to do, but due to some reasons(Check note in the last line) I cannot use CTRL +C . So I need to have some custom way to cause an interrupt.

In other terms: In following script, cleanup function is called when CTRL + C is pressed. Now need to modify this behavior so that cleanup function should be called when some keys like ENTER OR ESC is pressed.

cleanup() {

#do cleanup and exit
echo "Cleaning up..."
exit;

}
echo "Please enter your input:"
read input

    while true
        do
          echo "This is some other info MERGED with user input in loop + $input"
          sleep 2;
          echo "[Press CTRL C to exit...]"
          trap 'cleanup'  SIGINT
        done

Query:

  • Is it possible to use custom keys for causing interrupts in bash?
  • If possible, how to achieve it?
  • Note:

    Reason: This script is called from another C++ program which has its own trap handling. So the trap handling of this script is conflicting with the parent program and ultimately the terminal is getting hung. In my organization that program's code is frozen so I cannot change its behavior. I have to tweak this child script only.


    Following works ^M for ENTER and ^[ for ESC but may

    stty intr ^M 
    stty intr ^[ 
    

    but after cannot use ENTER to restore default

    stty intr ^C
    

    After comment, as the shell is interactive what about asking to continue instead using trap, also clean can be done in special EXIT trap.

    How do I prompt for Yes/No/Cancel input in a Linux shell script?

    or yet another solution using select

    echo "Do you want to continue?"
    PS3="Your choice: "
    
    select number in Y N;
    do
        case $REPLY in
        "N")
            echo "Exiting."
            exit
            ;;
        "Y")
            break
            ;;
        esac
    done
    # continue
    

    Here is a dirty trick which is doing my work just fine. read and case statement options are the key. Here I am timing out read command so that while true continues unless esc or enter is pressed.

    cleanup() {
    
    #do cleanup and exit
    echo "Cleaning up..."
    exit;
    
    }
    
    exitFunction()
    
    {
    echo "Exit function has been called..."
    exit 0;
    }
    mainFunction()
    {
        while true
            do
              echo "This is some other info MERGED with user input in loop"
              IFS=''
              read -s -N 1 -t 2 -p  "Press ESC TO EXIT or ENTER for cleanup" input
              case $input in
                    $'x0a' ) cleanup; break;;
                    $'e'   ) exitFunction;break;;
                          * ) main;break;;
              esac
            done
    }
    
    mainFunction
    
    链接地址: http://www.djcxy.com/p/25560.html

    上一篇: 如何将当前工作目录设置为脚本的目录?

    下一篇: 通过键(如ENTER或ESC)在bash中创建中断