create menu with x items depending on variable












0














I am trying to get a menu with x lines depending on a variable.



setup:



i have a network with x ip addresses



i want a user to get a menu where he can simply choose an ip address by entering a number:



for instance:



i have 4 ip adresses



ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4


i want the user to get a menu like



1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:


however, the number of ip addresses can variate.



I am trying to do this in a case:



echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt

1) echo ${!ipnum}
esac


but i cant get it into a loop










share|improve this question
























  • Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
    – fra-san
    Dec 11 at 11:39










  • I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
    – WingZero
    Dec 11 at 12:01


















0














I am trying to get a menu with x lines depending on a variable.



setup:



i have a network with x ip addresses



i want a user to get a menu where he can simply choose an ip address by entering a number:



for instance:



i have 4 ip adresses



ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4


i want the user to get a menu like



1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:


however, the number of ip addresses can variate.



I am trying to do this in a case:



echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt

1) echo ${!ipnum}
esac


but i cant get it into a loop










share|improve this question
























  • Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
    – fra-san
    Dec 11 at 11:39










  • I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
    – WingZero
    Dec 11 at 12:01
















0












0








0







I am trying to get a menu with x lines depending on a variable.



setup:



i have a network with x ip addresses



i want a user to get a menu where he can simply choose an ip address by entering a number:



for instance:



i have 4 ip adresses



ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4


i want the user to get a menu like



1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:


however, the number of ip addresses can variate.



I am trying to do this in a case:



echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt

1) echo ${!ipnum}
esac


but i cant get it into a loop










share|improve this question















I am trying to get a menu with x lines depending on a variable.



setup:



i have a network with x ip addresses



i want a user to get a menu where he can simply choose an ip address by entering a number:



for instance:



i have 4 ip adresses



ip1=192.168.1.1
ip2=192.168.1.2
ip3=192.168.1.3
ip4=192.168.1.4


i want the user to get a menu like



1. 192.168.1.1
2. 192.168.1.2
3. 192.168.1.3
4. 192.168.1.4
Please enter your choice:


however, the number of ip addresses can variate.



I am trying to do this in a case:



echo -n "Please enter your choice: "
read opt
case $opt in
ipnum=ip$opt

1) echo ${!ipnum}
esac


but i cant get it into a loop







shell-script






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 11 at 11:13









Ipor Sircer

10.5k11024




10.5k11024










asked Dec 11 at 10:47









WingZero

385




385












  • Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
    – fra-san
    Dec 11 at 11:39










  • I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
    – WingZero
    Dec 11 at 12:01




















  • Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
    – fra-san
    Dec 11 at 11:39










  • I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
    – WingZero
    Dec 11 at 12:01


















Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39




Any specific reason for using the case compound command? Wouldn't putting the IPs in an array and loop over it to print a menu be enough? E.g. for i in "${!ips[@]}"; do ... print menu item ... ; done; ... prompt ... ; read answer... ; ... use user's choice ...
– fra-san
Dec 11 at 11:39












I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01






I am fairly new to this, so i am still trying to understand all the functions and what to use when. I use case because i got an other menu working with it
– WingZero
Dec 11 at 12:01












2 Answers
2






active

oldest

votes


















1














In case you are using bash (or a similar shell):



ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )

select ip in "${ipnums[@]}"; do
case $ip in
"") echo 'Invalid choice' >&2 ;;
*) break
esac
done

printf 'You picked IP %sn' "$ip"


This would display a menu and a prompt like this:



1) 192.168.1.1
2) 192.168.1.2
3) 192.168.1.3
4) 192.168.1.4
#?


The prompt may be changed by setting PS3 to the prompt string to use.



The select loop verifies the input and loops until a valid input is had from the user.



After the loop, the IP address that was chosen is available in $ip.



If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):



select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done





share|improve this answer































    0














    In my case i figured it out.



    like @fra-san said why use case and not just print it and ask for the users choice.



    i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .






    share|improve this answer





















      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2funix.stackexchange.com%2fquestions%2f487310%2fcreate-menu-with-x-items-depending-on-variable%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      In case you are using bash (or a similar shell):



      ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )

      select ip in "${ipnums[@]}"; do
      case $ip in
      "") echo 'Invalid choice' >&2 ;;
      *) break
      esac
      done

      printf 'You picked IP %sn' "$ip"


      This would display a menu and a prompt like this:



      1) 192.168.1.1
      2) 192.168.1.2
      3) 192.168.1.3
      4) 192.168.1.4
      #?


      The prompt may be changed by setting PS3 to the prompt string to use.



      The select loop verifies the input and loops until a valid input is had from the user.



      After the loop, the IP address that was chosen is available in $ip.



      If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):



      select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done





      share|improve this answer




























        1














        In case you are using bash (or a similar shell):



        ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )

        select ip in "${ipnums[@]}"; do
        case $ip in
        "") echo 'Invalid choice' >&2 ;;
        *) break
        esac
        done

        printf 'You picked IP %sn' "$ip"


        This would display a menu and a prompt like this:



        1) 192.168.1.1
        2) 192.168.1.2
        3) 192.168.1.3
        4) 192.168.1.4
        #?


        The prompt may be changed by setting PS3 to the prompt string to use.



        The select loop verifies the input and loops until a valid input is had from the user.



        After the loop, the IP address that was chosen is available in $ip.



        If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):



        select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done





        share|improve this answer


























          1












          1








          1






          In case you are using bash (or a similar shell):



          ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )

          select ip in "${ipnums[@]}"; do
          case $ip in
          "") echo 'Invalid choice' >&2 ;;
          *) break
          esac
          done

          printf 'You picked IP %sn' "$ip"


          This would display a menu and a prompt like this:



          1) 192.168.1.1
          2) 192.168.1.2
          3) 192.168.1.3
          4) 192.168.1.4
          #?


          The prompt may be changed by setting PS3 to the prompt string to use.



          The select loop verifies the input and loops until a valid input is had from the user.



          After the loop, the IP address that was chosen is available in $ip.



          If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):



          select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done





          share|improve this answer














          In case you are using bash (or a similar shell):



          ipnums=( 192.168.1.1 192.168.1.2 192.168.1.3 192.168.1.4 )

          select ip in "${ipnums[@]}"; do
          case $ip in
          "") echo 'Invalid choice' >&2 ;;
          *) break
          esac
          done

          printf 'You picked IP %sn' "$ip"


          This would display a menu and a prompt like this:



          1) 192.168.1.1
          2) 192.168.1.2
          3) 192.168.1.3
          4) 192.168.1.4
          #?


          The prompt may be changed by setting PS3 to the prompt string to use.



          The select loop verifies the input and loops until a valid input is had from the user.



          After the loop, the IP address that was chosen is available in $ip.



          If you don't want to store your addresses in an array, you would have to list the relevant variables instead (this would not easily be generalised to N variables, which is why I suggested using an array in my answer to your other question):



          select ip in "$ip1" "$ip2" "$ip3" "$ip4"; do ...; done






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 11 at 13:09

























          answered Dec 11 at 12:55









          Kusalananda

          121k16228372




          121k16228372

























              0














              In my case i figured it out.



              like @fra-san said why use case and not just print it and ask for the users choice.



              i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .






              share|improve this answer


























                0














                In my case i figured it out.



                like @fra-san said why use case and not just print it and ask for the users choice.



                i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .






                share|improve this answer
























                  0












                  0








                  0






                  In my case i figured it out.



                  like @fra-san said why use case and not just print it and ask for the users choice.



                  i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .






                  share|improve this answer












                  In my case i figured it out.



                  like @fra-san said why use case and not just print it and ask for the users choice.



                  i just used an array to echo all ip addressing with an extra number in front, and when they enter the number it correspondences with the number in the variable, so i have my answer .







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 11 at 12:10









                  WingZero

                  385




                  385






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • 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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2funix.stackexchange.com%2fquestions%2f487310%2fcreate-menu-with-x-items-depending-on-variable%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

                      Morgemoulin

                      Scott Moir

                      Souastre