1

I've got a simple bash menu script that works almost perfect. But for some reason, if I don't exit out of the script using the "Exit" option OR by using CTRL+C BEFORE I close my terminal window, then the script goes into an infinite loop and CPU usage goes up from 4% to 50%! I then have to kill the script using "kill -9 bash" to get my CPU usage to go back down again. But if I exit the script using "Exit" or "CTRL+C" then it works just fine.

Of course in a perfect world I would just always Exit first before closing my terminal session, but once I put this into my workflow, there will most certainly come a time when I forget and close the terminal session out without Exiting the script first. And then I'm gonna have my CPU usage going up in the background.

I know this has something to do with using "while true; do" because I didn't have this issue before I started using that. But I need "while true; do" otherwise the SubMenu will not show the Main Menu options when going back to the Main Menu. So that had to be implemented.

Any ideas?

#!/bin/bash

main_menu () {
while true; do
  clear
  PS3='Select an option: '
  options=("Option1" "Option2" "SubMenu" "Exit")

  select opt in "${options[@]}";
  do
      case $opt in
          "Option1")
              echo ""
              echo Option1
              read -p ""
              clear
              break
           ;;
          "Option2")
              echo ""
              echo Option2
              read -p ""
              clear
              break
           ;;
          "SubMenu")
              while true; do
              clear
              PS3='Select an option: '
              options=("SubMenu Option1" "SubMenu Option2" "Main Menu")
              select opt in "${options[@]}"
              do
                case $opt in
                    "SubMenu Option1")
                        echo ""
                        echo "Sub-Menu Option1"
                        read -p ""
                        clear
                        break
                        ;;
                    "SubMenu Option2")
                        echo ""
                        echo "Sub-Menu Option2"
                        read -p ""
                        clear
                        break
                        ;;
                    "Main Menu")
                        break 3
                        ;;
                    *) echo "invalid option"
                       ;;
              esac
            done
          done
        ;;
          "Exit")
              exit
           ;;
          *) echo "invalid option"

           ;;
      esac
  done
done
}

main_menu
4
  • options2 should be used in the sub menu Commented May 29, 2019 at 11:00
  • Try running the script with /bin/bash -v Commented May 29, 2019 at 11:15
  • user1133275, JL2210, I tried both of those suggestions but to no avail. Commented May 29, 2019 at 11:26
  • I cannot replicate your problem on Ubuntu 19.04, what bash version, linux distribution are you using ? Commented Oct 6, 2019 at 4:39

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.