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
  • options2 should be used in the sub menu – user1133275 May 29 at 11:00
  • Try running the script with /bin/bash -v – JL2210 May 29 at 11:15
  • user1133275, JL2210, I tried both of those suggestions but to no avail. – XenonKilla May 29 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.