Skip to content
Advertisement

How can I build a Trap function with any key

This function exits my script when I press cntr+C, how can i modify it to make it works when I press enter?

trap ctrl_c INT

function ctrl_c(){
    clear
    echo -e "n${red}[!] Saliendo...n${end}"
    rm *.txt 2>/dev/null
    tput cnorm; exit 1
}

Advertisement

Answer

This might help:

#!/bin/bash

# map Return/Enter/Ctrl+m in your terminal to interrupt signal
stty intr ^M 

ctrl_c(){ echo 'Someone pressed Return/Enter/Ctrl+m.'; }

# Restore the default at the end of the program
back2default(){ stty intr ^C; }

trap ctrl_c INT
trap back2default EXIT

# A placeholder for more meaningful code
sleep 10
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement