1

I don't know what's wrong but this script doesn't work. It should be always goes back to menu everytime I press CTRL+C.

#!/bin/bash

func_info()
{
    clear
    echo "This is info page."
    read -p "Press CTRL+C to back to menu or press enter exit..."
    exit
}

func_menu()
{
    clear
    trap - SIGINT
    echo "This is menu page."
    read -p "Press enter to go to info page..."
    trap func_menu SIGINT
    func_info
}

func_menu

It works for the first CTRL+C but the second times it just doesn't works.

I'm new to this so please don't judge me ;)

Any helps appreciated :) Thanks.

EDIT:

Actually, I found this works

#!/bin/bash

func_info()
{
    clear
    echo "This is info page."
    read -p "Press CTRL+C to back to menu or press enter exit..."
    exit
}

func_menu()
{
    clear
    echo "This is menu page."
    read -p "Press enter to go to info page..."
    ( trap exit SIGINT; func_info )
    func_menu
}

func_menu

But is that OK?

3
  • Once you are inside SIGINT handler you stay there.
    – KamilCuk
    Apr 4, 2020 at 2:51
  • @KamilCuk Is there any way to make this works?
    – Rebelion
    Apr 4, 2020 at 2:53
  • bash doesn't interrupt read and executes SIGINT handler in a subshell where signals are ignored. I think this is a bug Apr 4, 2020 at 5:44

1 Answer 1

1

As a rule of thumb, you want to do as little as possible during signal handling. Especially if you're intending to recover from the signal instead of exiting, it is generally preferable to simply record that the signal was sent and then actually handle the signal in your "normal" code.

So instead of recursively calling functions (inside traps) in order to navigate, let's keep track of the current page and display it in a loop. Then, all the SIGINT handler has to do is update the current-page variable.

Here's a working demo of a three-page flow like your example (using select to navigate, but any approach would work if you don't like select):

pages=(info news contact)
page=

info() { echo "For info, see the news page"; }
news() { echo "$(date +%Y-%m-%d): No news today"; }
contact() { echo "We cannot be reached."; }

while true; do
  trap - SIGINT
  clear
  if [[ -n "$page" ]]; then
    trap 'page=; continue' SIGINT
    echo "This is the ${page} page"
    "$page" # invokes the function named $page
    ( read -p $'\nPress CTRL+C to go back to menu or press enter to exit...' )
    exit
  else
    echo "This is the menu page"
    echo "Select a page number and press enter to go there."
    select page in "${pages[@]}"; do
      if [[ -n "$page" ]]; then break; fi
    done
  fi
done
  • Clear the trap at the beginning of the loop, so that ^C exits the script from the menu page
  • When displaying a page, configure a trap that resets page on SIGINT/^C
  • read in a subshell
    • as you found, a subshell helps, because SIGINT/^C interrupts the subshell as a whole rather than read specifically
    • and only exit if the read succeeds

Give it a whirl! It works great on my system (Bash 4.3)

2
  • Nice one. The answer doesn't explain why read should be executed in a subshell, but the maintainer kind of provides that information here. May 20, 2020 at 9:16
  • 1
    Thanks for the pointer! I looked around for a citation for why read needs to be run from a subshell, but didn't immediately find anything canonical.
    – dimo414
    May 20, 2020 at 18:58

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.