bash - How can I re-display selection menu after a selection is chosen and performed

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I am a beginner to script. I am making a tool script for my theme with 2 functions: Check for update, reinstall theme



So here is the code for selection menu:



PS3='Choose an option: '
options=("Check for update" "Reinstall theme")
select opt in "$options[@]"
do
case $opt in
"Check for update")
echo "Checking update"
;;
"Reinstall theme")
echo "Reinstalling"
;;
*) echo invalid option;;
esac
done


When running it appear like this



1) Check for update
2) Reinstall theme
Choose an option:


I type 1 and enter, the check for update command is performed



The problem is when it finished performing the script, it re-display "Choose an option:" not with the menu. So it can make users hard to choose without the menu (especially after a long script)



1) Check for update
2) Reinstall theme
Choose an option: 1
Checking update
Choose an option:


So how can I re-display the menu after an option is performed










share|improve this question



























    up vote
    3
    down vote

    favorite












    I am a beginner to script. I am making a tool script for my theme with 2 functions: Check for update, reinstall theme



    So here is the code for selection menu:



    PS3='Choose an option: '
    options=("Check for update" "Reinstall theme")
    select opt in "$options[@]"
    do
    case $opt in
    "Check for update")
    echo "Checking update"
    ;;
    "Reinstall theme")
    echo "Reinstalling"
    ;;
    *) echo invalid option;;
    esac
    done


    When running it appear like this



    1) Check for update
    2) Reinstall theme
    Choose an option:


    I type 1 and enter, the check for update command is performed



    The problem is when it finished performing the script, it re-display "Choose an option:" not with the menu. So it can make users hard to choose without the menu (especially after a long script)



    1) Check for update
    2) Reinstall theme
    Choose an option: 1
    Checking update
    Choose an option:


    So how can I re-display the menu after an option is performed










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am a beginner to script. I am making a tool script for my theme with 2 functions: Check for update, reinstall theme



      So here is the code for selection menu:



      PS3='Choose an option: '
      options=("Check for update" "Reinstall theme")
      select opt in "$options[@]"
      do
      case $opt in
      "Check for update")
      echo "Checking update"
      ;;
      "Reinstall theme")
      echo "Reinstalling"
      ;;
      *) echo invalid option;;
      esac
      done


      When running it appear like this



      1) Check for update
      2) Reinstall theme
      Choose an option:


      I type 1 and enter, the check for update command is performed



      The problem is when it finished performing the script, it re-display "Choose an option:" not with the menu. So it can make users hard to choose without the menu (especially after a long script)



      1) Check for update
      2) Reinstall theme
      Choose an option: 1
      Checking update
      Choose an option:


      So how can I re-display the menu after an option is performed










      share|improve this question















      I am a beginner to script. I am making a tool script for my theme with 2 functions: Check for update, reinstall theme



      So here is the code for selection menu:



      PS3='Choose an option: '
      options=("Check for update" "Reinstall theme")
      select opt in "$options[@]"
      do
      case $opt in
      "Check for update")
      echo "Checking update"
      ;;
      "Reinstall theme")
      echo "Reinstalling"
      ;;
      *) echo invalid option;;
      esac
      done


      When running it appear like this



      1) Check for update
      2) Reinstall theme
      Choose an option:


      I type 1 and enter, the check for update command is performed



      The problem is when it finished performing the script, it re-display "Choose an option:" not with the menu. So it can make users hard to choose without the menu (especially after a long script)



      1) Check for update
      2) Reinstall theme
      Choose an option: 1
      Checking update
      Choose an option:


      So how can I re-display the menu after an option is performed







      bash select






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 3 '16 at 9:41









      Cyrus

      7,1312835




      7,1312835










      asked Jul 1 '16 at 16:39









      superquanganh

      7233812




      7233812




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          I'm guessing you really want something like this:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme")

          echo "Choose an option:"
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          *) echo "What's that?" >&2
          esac
          done

          echo "Doing other things..."

          echo "Are we done?"
          select opt in "Yes" "No"; do
          case $REPLY in
          1) break 2 ;;
          2) break ;;
          *) echo "Look, it's a simple question..." >&2
          esac
          done
          done


          I've separated out the tasks into separate function to keep the first case statement smaller. I've also used $REPLY rather than the option string in the case statements since this is shorter and won't break if you decide to change them but forget to update them in both places. I'm also choosing to not touch PS3 as that may affect later select calls in the script. If I wanted a different prompt, I would set it once in and leave it (maybe PS3="Your choice: "). This would give a script with multiple questions a more uniform feel.



          I've added an outer loop that iterates over everything until the user is done. You need this loop to re-display the question in the first select statement.



          I've added break to the case statements, otherwise there's no way to exit other than interrupting the script.



          The purpose of a select is to get an answer to one question from the user, not really to be the main event-loop of a script (by itself). In general, a select-case should really only set a variable or call a function and then carry on.



          A shorter version that incorporates a "Quit" option in the first select:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme" "Quit")

          echo "Choose an option: "
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          3) break 2 ;;
          *) echo "What's that?" >&2
          esac
          done
          done

          echo "Bye bye!"





          share|improve this answer






















          • I forget to add another option to quit, but im trying to fix not re-displaying menu issue
            – superquanganh
            Jul 2 '16 at 7:57










          • But let me try that, thanks
            – superquanganh
            Jul 2 '16 at 7:57










          • @superquanganh see updated answer.
            – Kusalananda
            Jul 2 '16 at 8:09






          • 1




            It worked, thanks
            – superquanganh
            Jul 2 '16 at 13:11

















          up vote
          1
          down vote













          Your do loop is endless and your select statement is outside of it. The script executes the select statement once and then stays in the do loop checking for case $opt over and over. My recommendation would be to put break after your case statement like this:



          esac
          break
          done


          Then, if you really want the whole script to repeat itself over and over again, create another loop that encloses everything from the select statement to the done statement.






          share|improve this answer




















          • How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
            – superquanganh
            Jul 2 '16 at 2:24










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f293340%2fbash-how-can-i-re-display-selection-menu-after-a-selection-is-chosen-and-perfo%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote



          accepted










          I'm guessing you really want something like this:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme")

          echo "Choose an option:"
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          *) echo "What's that?" >&2
          esac
          done

          echo "Doing other things..."

          echo "Are we done?"
          select opt in "Yes" "No"; do
          case $REPLY in
          1) break 2 ;;
          2) break ;;
          *) echo "Look, it's a simple question..." >&2
          esac
          done
          done


          I've separated out the tasks into separate function to keep the first case statement smaller. I've also used $REPLY rather than the option string in the case statements since this is shorter and won't break if you decide to change them but forget to update them in both places. I'm also choosing to not touch PS3 as that may affect later select calls in the script. If I wanted a different prompt, I would set it once in and leave it (maybe PS3="Your choice: "). This would give a script with multiple questions a more uniform feel.



          I've added an outer loop that iterates over everything until the user is done. You need this loop to re-display the question in the first select statement.



          I've added break to the case statements, otherwise there's no way to exit other than interrupting the script.



          The purpose of a select is to get an answer to one question from the user, not really to be the main event-loop of a script (by itself). In general, a select-case should really only set a variable or call a function and then carry on.



          A shorter version that incorporates a "Quit" option in the first select:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme" "Quit")

          echo "Choose an option: "
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          3) break 2 ;;
          *) echo "What's that?" >&2
          esac
          done
          done

          echo "Bye bye!"





          share|improve this answer






















          • I forget to add another option to quit, but im trying to fix not re-displaying menu issue
            – superquanganh
            Jul 2 '16 at 7:57










          • But let me try that, thanks
            – superquanganh
            Jul 2 '16 at 7:57










          • @superquanganh see updated answer.
            – Kusalananda
            Jul 2 '16 at 8:09






          • 1




            It worked, thanks
            – superquanganh
            Jul 2 '16 at 13:11














          up vote
          5
          down vote



          accepted










          I'm guessing you really want something like this:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme")

          echo "Choose an option:"
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          *) echo "What's that?" >&2
          esac
          done

          echo "Doing other things..."

          echo "Are we done?"
          select opt in "Yes" "No"; do
          case $REPLY in
          1) break 2 ;;
          2) break ;;
          *) echo "Look, it's a simple question..." >&2
          esac
          done
          done


          I've separated out the tasks into separate function to keep the first case statement smaller. I've also used $REPLY rather than the option string in the case statements since this is shorter and won't break if you decide to change them but forget to update them in both places. I'm also choosing to not touch PS3 as that may affect later select calls in the script. If I wanted a different prompt, I would set it once in and leave it (maybe PS3="Your choice: "). This would give a script with multiple questions a more uniform feel.



          I've added an outer loop that iterates over everything until the user is done. You need this loop to re-display the question in the first select statement.



          I've added break to the case statements, otherwise there's no way to exit other than interrupting the script.



          The purpose of a select is to get an answer to one question from the user, not really to be the main event-loop of a script (by itself). In general, a select-case should really only set a variable or call a function and then carry on.



          A shorter version that incorporates a "Quit" option in the first select:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme" "Quit")

          echo "Choose an option: "
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          3) break 2 ;;
          *) echo "What's that?" >&2
          esac
          done
          done

          echo "Bye bye!"





          share|improve this answer






















          • I forget to add another option to quit, but im trying to fix not re-displaying menu issue
            – superquanganh
            Jul 2 '16 at 7:57










          • But let me try that, thanks
            – superquanganh
            Jul 2 '16 at 7:57










          • @superquanganh see updated answer.
            – Kusalananda
            Jul 2 '16 at 8:09






          • 1




            It worked, thanks
            – superquanganh
            Jul 2 '16 at 13:11












          up vote
          5
          down vote



          accepted







          up vote
          5
          down vote



          accepted






          I'm guessing you really want something like this:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme")

          echo "Choose an option:"
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          *) echo "What's that?" >&2
          esac
          done

          echo "Doing other things..."

          echo "Are we done?"
          select opt in "Yes" "No"; do
          case $REPLY in
          1) break 2 ;;
          2) break ;;
          *) echo "Look, it's a simple question..." >&2
          esac
          done
          done


          I've separated out the tasks into separate function to keep the first case statement smaller. I've also used $REPLY rather than the option string in the case statements since this is shorter and won't break if you decide to change them but forget to update them in both places. I'm also choosing to not touch PS3 as that may affect later select calls in the script. If I wanted a different prompt, I would set it once in and leave it (maybe PS3="Your choice: "). This would give a script with multiple questions a more uniform feel.



          I've added an outer loop that iterates over everything until the user is done. You need this loop to re-display the question in the first select statement.



          I've added break to the case statements, otherwise there's no way to exit other than interrupting the script.



          The purpose of a select is to get an answer to one question from the user, not really to be the main event-loop of a script (by itself). In general, a select-case should really only set a variable or call a function and then carry on.



          A shorter version that incorporates a "Quit" option in the first select:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme" "Quit")

          echo "Choose an option: "
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          3) break 2 ;;
          *) echo "What's that?" >&2
          esac
          done
          done

          echo "Bye bye!"





          share|improve this answer














          I'm guessing you really want something like this:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme")

          echo "Choose an option:"
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          *) echo "What's that?" >&2
          esac
          done

          echo "Doing other things..."

          echo "Are we done?"
          select opt in "Yes" "No"; do
          case $REPLY in
          1) break 2 ;;
          2) break ;;
          *) echo "Look, it's a simple question..." >&2
          esac
          done
          done


          I've separated out the tasks into separate function to keep the first case statement smaller. I've also used $REPLY rather than the option string in the case statements since this is shorter and won't break if you decide to change them but forget to update them in both places. I'm also choosing to not touch PS3 as that may affect later select calls in the script. If I wanted a different prompt, I would set it once in and leave it (maybe PS3="Your choice: "). This would give a script with multiple questions a more uniform feel.



          I've added an outer loop that iterates over everything until the user is done. You need this loop to re-display the question in the first select statement.



          I've added break to the case statements, otherwise there's no way to exit other than interrupting the script.



          The purpose of a select is to get an answer to one question from the user, not really to be the main event-loop of a script (by itself). In general, a select-case should really only set a variable or call a function and then carry on.



          A shorter version that incorporates a "Quit" option in the first select:



          check_update () 
          echo "Checking update"


          reinstall_theme ()
          echo "Reinstalling theme"


          while true; do
          options=("Check for update" "Reinstall theme" "Quit")

          echo "Choose an option: "
          select opt in "$options[@]"; do
          case $REPLY in
          1) check_update; break ;;
          2) reinstall_theme; break ;;
          3) break 2 ;;
          *) echo "What's that?" >&2
          esac
          done
          done

          echo "Bye bye!"






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 13 mins ago

























          answered Jul 2 '16 at 7:52









          Kusalananda

          111k15216342




          111k15216342











          • I forget to add another option to quit, but im trying to fix not re-displaying menu issue
            – superquanganh
            Jul 2 '16 at 7:57










          • But let me try that, thanks
            – superquanganh
            Jul 2 '16 at 7:57










          • @superquanganh see updated answer.
            – Kusalananda
            Jul 2 '16 at 8:09






          • 1




            It worked, thanks
            – superquanganh
            Jul 2 '16 at 13:11
















          • I forget to add another option to quit, but im trying to fix not re-displaying menu issue
            – superquanganh
            Jul 2 '16 at 7:57










          • But let me try that, thanks
            – superquanganh
            Jul 2 '16 at 7:57










          • @superquanganh see updated answer.
            – Kusalananda
            Jul 2 '16 at 8:09






          • 1




            It worked, thanks
            – superquanganh
            Jul 2 '16 at 13:11















          I forget to add another option to quit, but im trying to fix not re-displaying menu issue
          – superquanganh
          Jul 2 '16 at 7:57




          I forget to add another option to quit, but im trying to fix not re-displaying menu issue
          – superquanganh
          Jul 2 '16 at 7:57












          But let me try that, thanks
          – superquanganh
          Jul 2 '16 at 7:57




          But let me try that, thanks
          – superquanganh
          Jul 2 '16 at 7:57












          @superquanganh see updated answer.
          – Kusalananda
          Jul 2 '16 at 8:09




          @superquanganh see updated answer.
          – Kusalananda
          Jul 2 '16 at 8:09




          1




          1




          It worked, thanks
          – superquanganh
          Jul 2 '16 at 13:11




          It worked, thanks
          – superquanganh
          Jul 2 '16 at 13:11












          up vote
          1
          down vote













          Your do loop is endless and your select statement is outside of it. The script executes the select statement once and then stays in the do loop checking for case $opt over and over. My recommendation would be to put break after your case statement like this:



          esac
          break
          done


          Then, if you really want the whole script to repeat itself over and over again, create another loop that encloses everything from the select statement to the done statement.






          share|improve this answer




















          • How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
            – superquanganh
            Jul 2 '16 at 2:24














          up vote
          1
          down vote













          Your do loop is endless and your select statement is outside of it. The script executes the select statement once and then stays in the do loop checking for case $opt over and over. My recommendation would be to put break after your case statement like this:



          esac
          break
          done


          Then, if you really want the whole script to repeat itself over and over again, create another loop that encloses everything from the select statement to the done statement.






          share|improve this answer




















          • How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
            – superquanganh
            Jul 2 '16 at 2:24












          up vote
          1
          down vote










          up vote
          1
          down vote









          Your do loop is endless and your select statement is outside of it. The script executes the select statement once and then stays in the do loop checking for case $opt over and over. My recommendation would be to put break after your case statement like this:



          esac
          break
          done


          Then, if you really want the whole script to repeat itself over and over again, create another loop that encloses everything from the select statement to the done statement.






          share|improve this answer












          Your do loop is endless and your select statement is outside of it. The script executes the select statement once and then stays in the do loop checking for case $opt over and over. My recommendation would be to put break after your case statement like this:



          esac
          break
          done


          Then, if you really want the whole script to repeat itself over and over again, create another loop that encloses everything from the select statement to the done statement.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jul 1 '16 at 17:57









          bashBedlam

          36513




          36513











          • How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
            – superquanganh
            Jul 2 '16 at 2:24
















          • How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
            – superquanganh
            Jul 2 '16 at 2:24















          How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
          – superquanganh
          Jul 2 '16 at 2:24




          How can i do that? I want it to repeat endlessly until users want to quit (I will add quit option later)
          – superquanganh
          Jul 2 '16 at 2:24

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f293340%2fbash-how-can-i-re-display-selection-menu-after-a-selection-is-chosen-and-perfo%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Peggy Mitchell

          The Forum (Inglewood, California)

          Palaiologos