Bash Shell Script with Menu












3















I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.



If someone could help me out a bit, thanks.



#!/bin/bashe
echo -n "Name please? "
read name
echo "Menu for $name
1. Display a long listing of the current directory
2. Display who is logged on the system
3. Display the current date and time
4. Quit "
read input
input1="1"
input2="2"
input3=$(date)
input4=$(exit)
while [ "$input" = "3" ]
do
echo "Current date and time: $input3"
done

while [ "$input" = "4" ]
do
echo "Goodbye $input4"
done









share|improve this question





























    3















    I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.



    If someone could help me out a bit, thanks.



    #!/bin/bashe
    echo -n "Name please? "
    read name
    echo "Menu for $name
    1. Display a long listing of the current directory
    2. Display who is logged on the system
    3. Display the current date and time
    4. Quit "
    read input
    input1="1"
    input2="2"
    input3=$(date)
    input4=$(exit)
    while [ "$input" = "3" ]
    do
    echo "Current date and time: $input3"
    done

    while [ "$input" = "4" ]
    do
    echo "Goodbye $input4"
    done









    share|improve this question



























      3












      3








      3


      1






      I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.



      If someone could help me out a bit, thanks.



      #!/bin/bashe
      echo -n "Name please? "
      read name
      echo "Menu for $name
      1. Display a long listing of the current directory
      2. Display who is logged on the system
      3. Display the current date and time
      4. Quit "
      read input
      input1="1"
      input2="2"
      input3=$(date)
      input4=$(exit)
      while [ "$input" = "3" ]
      do
      echo "Current date and time: $input3"
      done

      while [ "$input" = "4" ]
      do
      echo "Goodbye $input4"
      done









      share|improve this question
















      I've written a bash shell script (code provided below) that gives the user 4 options. However I'm having a little trouble with the code. Right now when they select option 3, to show the date. It loops over and over again. I have to close the terminal window to stop it because it's an infinite loop. How would I prevent this? Also quit doesn't seem to be working either.



      If someone could help me out a bit, thanks.



      #!/bin/bashe
      echo -n "Name please? "
      read name
      echo "Menu for $name
      1. Display a long listing of the current directory
      2. Display who is logged on the system
      3. Display the current date and time
      4. Quit "
      read input
      input1="1"
      input2="2"
      input3=$(date)
      input4=$(exit)
      while [ "$input" = "3" ]
      do
      echo "Current date and time: $input3"
      done

      while [ "$input" = "4" ]
      do
      echo "Goodbye $input4"
      done






      linux bash






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 '14 at 9:00









      Der Hochstapler

      68k49230285




      68k49230285










      asked Apr 12 '11 at 21:21









      rjdelightrjdelight

      16112




      16112






















          3 Answers
          3






          active

          oldest

          votes


















          9














          A compact version:



          options=(
          "Display a long listing of the current directory"
          "Display who is logged on the system"
          "Display the current date and time"
          "Quit"
          )

          PS3="Enter a number (1-${#options[@]}): "

          select option in "${options[@]}"; do
          case "$REPLY" in
          1) ls -l ;;
          2) who ;;
          3) date ;;
          4) break ;;
          esac
          done





          share|improve this answer





















          • 1





            Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

            – Hai Vu
            Apr 13 '11 at 5:37





















          3














          I guess it's due to the while loop ;)



          The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.



          My version of the script would be:



          #!/bin/bash
          echo -n "Name please? "
          read name
          input=""
          while [ "$input" != "4" ]; do
          echo "Menu for $name
          1. Display a long listing of the current directory
          2. Display who is logged on the system
          3. Display the current date and time
          4. Quit "
          read input
          input1="1"
          input2="2"
          input3=$(date)
          input4=$(exit)
          if [ "$input" = "3" ]; then
          echo "Current date and time: $input3"
          fi

          if [ "$input" = "4" ]; then
          echo "Goodbye $input4"
          fi
          done





          share|improve this answer

































            0














            case is here to help you, try



             # cat list.sh
            #!/bin/bash
            echo -n "Name please? "
            read name
            echo "Menu for $name"
            echo -e "Enter ( 1 ) to Display a long listing of the current directory nEnter ( 2 ) to Display who is logged on the systemnEnter ( 3 ) to Display the current date and timenEnter ( 4 ) to Quit"
            read en
            case "$en" in
            1)ls -lR;;
            2)who;;
            3)date;;
            4)echo "Good Bye..."
            sleep 0;;
            *)echo "Wrong Option selected" ;;
            esac


            Output:



             # ./list.sh
            Name please? Ranjith
            Menu for Ranjith
            Enter ( 1 ) to Display a long listing of the current directory
            Enter ( 2 ) to Display who is logged on the system
            Enter ( 3 ) to Display the current date and time
            Enter ( 4 ) to Quit


            If you enter other options then the menu shows, then



             # ./list.sh
            Name please? Ranjith
            Menu for Ranjith
            Enter ( 1 ) to Display a long listing of the current directory
            Enter ( 2 ) to Display who is logged on the system
            Enter ( 3 ) to Display the current date and time
            Enter ( 4 ) to Quit
            5
            Wrong Option selected


            ...






            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "3"
              };
              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f270074%2fbash-shell-script-with-menu%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              9














              A compact version:



              options=(
              "Display a long listing of the current directory"
              "Display who is logged on the system"
              "Display the current date and time"
              "Quit"
              )

              PS3="Enter a number (1-${#options[@]}): "

              select option in "${options[@]}"; do
              case "$REPLY" in
              1) ls -l ;;
              2) who ;;
              3) date ;;
              4) break ;;
              esac
              done





              share|improve this answer





















              • 1





                Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

                – Hai Vu
                Apr 13 '11 at 5:37


















              9














              A compact version:



              options=(
              "Display a long listing of the current directory"
              "Display who is logged on the system"
              "Display the current date and time"
              "Quit"
              )

              PS3="Enter a number (1-${#options[@]}): "

              select option in "${options[@]}"; do
              case "$REPLY" in
              1) ls -l ;;
              2) who ;;
              3) date ;;
              4) break ;;
              esac
              done





              share|improve this answer





















              • 1





                Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

                – Hai Vu
                Apr 13 '11 at 5:37
















              9












              9








              9







              A compact version:



              options=(
              "Display a long listing of the current directory"
              "Display who is logged on the system"
              "Display the current date and time"
              "Quit"
              )

              PS3="Enter a number (1-${#options[@]}): "

              select option in "${options[@]}"; do
              case "$REPLY" in
              1) ls -l ;;
              2) who ;;
              3) date ;;
              4) break ;;
              esac
              done





              share|improve this answer















              A compact version:



              options=(
              "Display a long listing of the current directory"
              "Display who is logged on the system"
              "Display the current date and time"
              "Quit"
              )

              PS3="Enter a number (1-${#options[@]}): "

              select option in "${options[@]}"; do
              case "$REPLY" in
              1) ls -l ;;
              2) who ;;
              3) date ;;
              4) break ;;
              esac
              done






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 15 at 1:00

























              answered Apr 13 '11 at 0:21









              glenn jackmanglenn jackman

              16.1k22645




              16.1k22645








              • 1





                Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

                – Hai Vu
                Apr 13 '11 at 5:37
















              • 1





                Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

                – Hai Vu
                Apr 13 '11 at 5:37










              1




              1





              Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

              – Hai Vu
              Apr 13 '11 at 5:37







              Compact, to the point. May I suggest this line before the select command: PS3="Enter a number (1-4): "

              – Hai Vu
              Apr 13 '11 at 5:37















              3














              I guess it's due to the while loop ;)



              The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.



              My version of the script would be:



              #!/bin/bash
              echo -n "Name please? "
              read name
              input=""
              while [ "$input" != "4" ]; do
              echo "Menu for $name
              1. Display a long listing of the current directory
              2. Display who is logged on the system
              3. Display the current date and time
              4. Quit "
              read input
              input1="1"
              input2="2"
              input3=$(date)
              input4=$(exit)
              if [ "$input" = "3" ]; then
              echo "Current date and time: $input3"
              fi

              if [ "$input" = "4" ]; then
              echo "Goodbye $input4"
              fi
              done





              share|improve this answer






























                3














                I guess it's due to the while loop ;)



                The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.



                My version of the script would be:



                #!/bin/bash
                echo -n "Name please? "
                read name
                input=""
                while [ "$input" != "4" ]; do
                echo "Menu for $name
                1. Display a long listing of the current directory
                2. Display who is logged on the system
                3. Display the current date and time
                4. Quit "
                read input
                input1="1"
                input2="2"
                input3=$(date)
                input4=$(exit)
                if [ "$input" = "3" ]; then
                echo "Current date and time: $input3"
                fi

                if [ "$input" = "4" ]; then
                echo "Goodbye $input4"
                fi
                done





                share|improve this answer




























                  3












                  3








                  3







                  I guess it's due to the while loop ;)



                  The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.



                  My version of the script would be:



                  #!/bin/bash
                  echo -n "Name please? "
                  read name
                  input=""
                  while [ "$input" != "4" ]; do
                  echo "Menu for $name
                  1. Display a long listing of the current directory
                  2. Display who is logged on the system
                  3. Display the current date and time
                  4. Quit "
                  read input
                  input1="1"
                  input2="2"
                  input3=$(date)
                  input4=$(exit)
                  if [ "$input" = "3" ]; then
                  echo "Current date and time: $input3"
                  fi

                  if [ "$input" = "4" ]; then
                  echo "Goodbye $input4"
                  fi
                  done





                  share|improve this answer















                  I guess it's due to the while loop ;)



                  The value of $input doesn't change inside of the loop. Either you insert something like a read input into the loop or you change the while ... do into a if ... then. If I understand the intention of your script correctly you don't need those while loops where they are now. You need one big while loop covering everything from echo "Menu for $name till the end.



                  My version of the script would be:



                  #!/bin/bash
                  echo -n "Name please? "
                  read name
                  input=""
                  while [ "$input" != "4" ]; do
                  echo "Menu for $name
                  1. Display a long listing of the current directory
                  2. Display who is logged on the system
                  3. Display the current date and time
                  4. Quit "
                  read input
                  input1="1"
                  input2="2"
                  input3=$(date)
                  input4=$(exit)
                  if [ "$input" = "3" ]; then
                  echo "Current date and time: $input3"
                  fi

                  if [ "$input" = "4" ]; then
                  echo "Goodbye $input4"
                  fi
                  done






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 12 '11 at 21:35

























                  answered Apr 12 '11 at 21:30









                  bmkbmk

                  1,5991011




                  1,5991011























                      0














                      case is here to help you, try



                       # cat list.sh
                      #!/bin/bash
                      echo -n "Name please? "
                      read name
                      echo "Menu for $name"
                      echo -e "Enter ( 1 ) to Display a long listing of the current directory nEnter ( 2 ) to Display who is logged on the systemnEnter ( 3 ) to Display the current date and timenEnter ( 4 ) to Quit"
                      read en
                      case "$en" in
                      1)ls -lR;;
                      2)who;;
                      3)date;;
                      4)echo "Good Bye..."
                      sleep 0;;
                      *)echo "Wrong Option selected" ;;
                      esac


                      Output:



                       # ./list.sh
                      Name please? Ranjith
                      Menu for Ranjith
                      Enter ( 1 ) to Display a long listing of the current directory
                      Enter ( 2 ) to Display who is logged on the system
                      Enter ( 3 ) to Display the current date and time
                      Enter ( 4 ) to Quit


                      If you enter other options then the menu shows, then



                       # ./list.sh
                      Name please? Ranjith
                      Menu for Ranjith
                      Enter ( 1 ) to Display a long listing of the current directory
                      Enter ( 2 ) to Display who is logged on the system
                      Enter ( 3 ) to Display the current date and time
                      Enter ( 4 ) to Quit
                      5
                      Wrong Option selected


                      ...






                      share|improve this answer




























                        0














                        case is here to help you, try



                         # cat list.sh
                        #!/bin/bash
                        echo -n "Name please? "
                        read name
                        echo "Menu for $name"
                        echo -e "Enter ( 1 ) to Display a long listing of the current directory nEnter ( 2 ) to Display who is logged on the systemnEnter ( 3 ) to Display the current date and timenEnter ( 4 ) to Quit"
                        read en
                        case "$en" in
                        1)ls -lR;;
                        2)who;;
                        3)date;;
                        4)echo "Good Bye..."
                        sleep 0;;
                        *)echo "Wrong Option selected" ;;
                        esac


                        Output:



                         # ./list.sh
                        Name please? Ranjith
                        Menu for Ranjith
                        Enter ( 1 ) to Display a long listing of the current directory
                        Enter ( 2 ) to Display who is logged on the system
                        Enter ( 3 ) to Display the current date and time
                        Enter ( 4 ) to Quit


                        If you enter other options then the menu shows, then



                         # ./list.sh
                        Name please? Ranjith
                        Menu for Ranjith
                        Enter ( 1 ) to Display a long listing of the current directory
                        Enter ( 2 ) to Display who is logged on the system
                        Enter ( 3 ) to Display the current date and time
                        Enter ( 4 ) to Quit
                        5
                        Wrong Option selected


                        ...






                        share|improve this answer


























                          0












                          0








                          0







                          case is here to help you, try



                           # cat list.sh
                          #!/bin/bash
                          echo -n "Name please? "
                          read name
                          echo "Menu for $name"
                          echo -e "Enter ( 1 ) to Display a long listing of the current directory nEnter ( 2 ) to Display who is logged on the systemnEnter ( 3 ) to Display the current date and timenEnter ( 4 ) to Quit"
                          read en
                          case "$en" in
                          1)ls -lR;;
                          2)who;;
                          3)date;;
                          4)echo "Good Bye..."
                          sleep 0;;
                          *)echo "Wrong Option selected" ;;
                          esac


                          Output:



                           # ./list.sh
                          Name please? Ranjith
                          Menu for Ranjith
                          Enter ( 1 ) to Display a long listing of the current directory
                          Enter ( 2 ) to Display who is logged on the system
                          Enter ( 3 ) to Display the current date and time
                          Enter ( 4 ) to Quit


                          If you enter other options then the menu shows, then



                           # ./list.sh
                          Name please? Ranjith
                          Menu for Ranjith
                          Enter ( 1 ) to Display a long listing of the current directory
                          Enter ( 2 ) to Display who is logged on the system
                          Enter ( 3 ) to Display the current date and time
                          Enter ( 4 ) to Quit
                          5
                          Wrong Option selected


                          ...






                          share|improve this answer













                          case is here to help you, try



                           # cat list.sh
                          #!/bin/bash
                          echo -n "Name please? "
                          read name
                          echo "Menu for $name"
                          echo -e "Enter ( 1 ) to Display a long listing of the current directory nEnter ( 2 ) to Display who is logged on the systemnEnter ( 3 ) to Display the current date and timenEnter ( 4 ) to Quit"
                          read en
                          case "$en" in
                          1)ls -lR;;
                          2)who;;
                          3)date;;
                          4)echo "Good Bye..."
                          sleep 0;;
                          *)echo "Wrong Option selected" ;;
                          esac


                          Output:



                           # ./list.sh
                          Name please? Ranjith
                          Menu for Ranjith
                          Enter ( 1 ) to Display a long listing of the current directory
                          Enter ( 2 ) to Display who is logged on the system
                          Enter ( 3 ) to Display the current date and time
                          Enter ( 4 ) to Quit


                          If you enter other options then the menu shows, then



                           # ./list.sh
                          Name please? Ranjith
                          Menu for Ranjith
                          Enter ( 1 ) to Display a long listing of the current directory
                          Enter ( 2 ) to Display who is logged on the system
                          Enter ( 3 ) to Display the current date and time
                          Enter ( 4 ) to Quit
                          5
                          Wrong Option selected


                          ...







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 5 '13 at 0:37









                          Ranjithkumar TRanjithkumar T

                          209136




                          209136






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Super User!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f270074%2fbash-shell-script-with-menu%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                              Alcedinidae

                              Can an atomic nucleus contain both particles and antiparticles? [duplicate]