Exit script called from within a menu without exiting the menu

menu

I have written this menu that calls forth several scripts. One of this script is

dbus-monitor --system

so it displays live traffic over dbus.

but when I want to exit this I normally do Ctrl+C, but that also exits my menu and I would like to just return to my menu.

is there a code that I can put after the dbus-moniter, when an exit is detected it starts my menu again?
my menu is just another .sh script

or ….

—————- clarify —————

i am not that advanced "yet" 😉 in scripting. this is the menu where i call my dbus script

select opt in "dbus Live Traffic" "option 2" "Main menu" "Quit"
    do
        case $opt in
            "dbus Live Traffic")
                curl -s -u lalala:hihihi ftp://ftp.somewhere.com/folder/dbuslivetraffic.sh | bash ;;   
            "option 2")
                do_something ;;   
            "Main menu")
                main_menu;;
            "Quit")
                quit_menu;;
        esac
        if [[ $opt != "Main menu" ]] || [[ $opt != "Quit" ]] ;
        then
            main_menu
        fi
    done

and this is the content of my dbuslivetraffic.sh

dbus-monitor --system

for now just this single line, but maybe in the near future more code will be added to this script.

i don't really understand where i need to put the TRAP function like suggested by @RoVo

Best Answer

You can run the command in a subshell and trap on SIGINT running kill 0 to kill the process group of the subshell only.

select opt in a b; do
    case $REPLY in
      1)
        (
          trap "kill -SIGINT 0" SIGINT
          sleep 10
        )
        ;;
      2)
        sleep 10
        ;;
    esac
done
  • Selecting (1) will let you use Ctrl+c without killing the menu.
  • Selecting (2) and pressing Ctrl+c will kill the menu, too.
Related Question