Loop to keep asking for the value until user enters unique value

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











up vote
1
down vote

favorite
1












I am creating a script for automated tasks regarding LVM. In the script, I want user to input the VG name and it has to be unique. How to create a loop so that if user enters a VG name that already exists in the system it doesn't move ahead and keeps asking for the VGname until its unique.



The function I am using for VG creation is mentioned below:



vg_create() grep -cw $vgname`

if [ $vg_match -eq 1 ]; then
echo -e "$vgname already exists. Kindly enter new name.n"
else
echo -e "$vgname doesn't exist in system and will be created.n"
fi
read -p "Enter the name of the physical volume on which volume group to be created: " pv2_name
printf "n"
vgcreate $vgname $pv2_name

printf "n"
printf "The new list of volume groups in the system are: n"
vgs






share|improve this question

























    up vote
    1
    down vote

    favorite
    1












    I am creating a script for automated tasks regarding LVM. In the script, I want user to input the VG name and it has to be unique. How to create a loop so that if user enters a VG name that already exists in the system it doesn't move ahead and keeps asking for the VGname until its unique.



    The function I am using for VG creation is mentioned below:



    vg_create() grep -cw $vgname`

    if [ $vg_match -eq 1 ]; then
    echo -e "$vgname already exists. Kindly enter new name.n"
    else
    echo -e "$vgname doesn't exist in system and will be created.n"
    fi
    read -p "Enter the name of the physical volume on which volume group to be created: " pv2_name
    printf "n"
    vgcreate $vgname $pv2_name

    printf "n"
    printf "The new list of volume groups in the system are: n"
    vgs






    share|improve this question























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I am creating a script for automated tasks regarding LVM. In the script, I want user to input the VG name and it has to be unique. How to create a loop so that if user enters a VG name that already exists in the system it doesn't move ahead and keeps asking for the VGname until its unique.



      The function I am using for VG creation is mentioned below:



      vg_create() grep -cw $vgname`

      if [ $vg_match -eq 1 ]; then
      echo -e "$vgname already exists. Kindly enter new name.n"
      else
      echo -e "$vgname doesn't exist in system and will be created.n"
      fi
      read -p "Enter the name of the physical volume on which volume group to be created: " pv2_name
      printf "n"
      vgcreate $vgname $pv2_name

      printf "n"
      printf "The new list of volume groups in the system are: n"
      vgs






      share|improve this question













      I am creating a script for automated tasks regarding LVM. In the script, I want user to input the VG name and it has to be unique. How to create a loop so that if user enters a VG name that already exists in the system it doesn't move ahead and keeps asking for the VGname until its unique.



      The function I am using for VG creation is mentioned below:



      vg_create() grep -cw $vgname`

      if [ $vg_match -eq 1 ]; then
      echo -e "$vgname already exists. Kindly enter new name.n"
      else
      echo -e "$vgname doesn't exist in system and will be created.n"
      fi
      read -p "Enter the name of the physical volume on which volume group to be created: " pv2_name
      printf "n"
      vgcreate $vgname $pv2_name

      printf "n"
      printf "The new list of volume groups in the system are: n"
      vgs








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 3 at 15:01









      ilkkachu

      47.3k668130




      47.3k668130









      asked Jul 3 at 14:56









      Vivek Dabas

      234




      234




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          In general:



          # loop until we get correct input from user
          while true; do
          # get input from user

          # check input

          # break if ok
          done


          Or, a bit more fleshed out:



          # loop until we get correct input from user
          while true; do
          read -r -p "Give your input: " answer

          # check $answer, break out of loop if ok, otherwise try again

          if pvs | awk 'NR > 2 print $2' | grep -qw -e "$answer"; then
          printf '%s already existsn' "$answer" >&2
          else
          break
          fi
          done


          Note: I have no idea what pvs does.






          share|improve this answer























          • pvs displays all the Phyical Volumes available in OS.
            – Vivek Dabas
            Jul 6 at 16:58

















          up vote
          1
          down vote













          Here's two different ways to check for the existence of a VG:



          1. attempt to read the VG directly vgs --readonly "$vgname"; if that command succeeds, the VG already exists.

          2. If the vgname is listed in the output from vgs, the VG already exists.

          Note that the second method specifically asks vgs to not print the heading and to only print the VG name field. The name is (on my system) often printed with leading and trailing spaces, which is why the grep expression looks the way it does.



          read -p "Enter the name of the volume group to be created: " vgname
          while vgs --readonly "$vgname" > /dev/null 2>&1
          do
          read -p "Enter the name of the volume group to be created: " vgname
          if vgs --noheadings -o vg_name | grep -q "^ *$vgname *$"
          then
          printf "That VG name is already taken; try something elsen" >&2
          fi
          done





          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',
            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%2f453246%2floop-to-keep-asking-for-the-value-until-user-enters-unique-value%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
            3
            down vote













            In general:



            # loop until we get correct input from user
            while true; do
            # get input from user

            # check input

            # break if ok
            done


            Or, a bit more fleshed out:



            # loop until we get correct input from user
            while true; do
            read -r -p "Give your input: " answer

            # check $answer, break out of loop if ok, otherwise try again

            if pvs | awk 'NR > 2 print $2' | grep -qw -e "$answer"; then
            printf '%s already existsn' "$answer" >&2
            else
            break
            fi
            done


            Note: I have no idea what pvs does.






            share|improve this answer























            • pvs displays all the Phyical Volumes available in OS.
              – Vivek Dabas
              Jul 6 at 16:58














            up vote
            3
            down vote













            In general:



            # loop until we get correct input from user
            while true; do
            # get input from user

            # check input

            # break if ok
            done


            Or, a bit more fleshed out:



            # loop until we get correct input from user
            while true; do
            read -r -p "Give your input: " answer

            # check $answer, break out of loop if ok, otherwise try again

            if pvs | awk 'NR > 2 print $2' | grep -qw -e "$answer"; then
            printf '%s already existsn' "$answer" >&2
            else
            break
            fi
            done


            Note: I have no idea what pvs does.






            share|improve this answer























            • pvs displays all the Phyical Volumes available in OS.
              – Vivek Dabas
              Jul 6 at 16:58












            up vote
            3
            down vote










            up vote
            3
            down vote









            In general:



            # loop until we get correct input from user
            while true; do
            # get input from user

            # check input

            # break if ok
            done


            Or, a bit more fleshed out:



            # loop until we get correct input from user
            while true; do
            read -r -p "Give your input: " answer

            # check $answer, break out of loop if ok, otherwise try again

            if pvs | awk 'NR > 2 print $2' | grep -qw -e "$answer"; then
            printf '%s already existsn' "$answer" >&2
            else
            break
            fi
            done


            Note: I have no idea what pvs does.






            share|improve this answer















            In general:



            # loop until we get correct input from user
            while true; do
            # get input from user

            # check input

            # break if ok
            done


            Or, a bit more fleshed out:



            # loop until we get correct input from user
            while true; do
            read -r -p "Give your input: " answer

            # check $answer, break out of loop if ok, otherwise try again

            if pvs | awk 'NR > 2 print $2' | grep -qw -e "$answer"; then
            printf '%s already existsn' "$answer" >&2
            else
            break
            fi
            done


            Note: I have no idea what pvs does.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jul 3 at 16:09


























            answered Jul 3 at 15:05









            Kusalananda

            101k13199312




            101k13199312











            • pvs displays all the Phyical Volumes available in OS.
              – Vivek Dabas
              Jul 6 at 16:58
















            • pvs displays all the Phyical Volumes available in OS.
              – Vivek Dabas
              Jul 6 at 16:58















            pvs displays all the Phyical Volumes available in OS.
            – Vivek Dabas
            Jul 6 at 16:58




            pvs displays all the Phyical Volumes available in OS.
            – Vivek Dabas
            Jul 6 at 16:58












            up vote
            1
            down vote













            Here's two different ways to check for the existence of a VG:



            1. attempt to read the VG directly vgs --readonly "$vgname"; if that command succeeds, the VG already exists.

            2. If the vgname is listed in the output from vgs, the VG already exists.

            Note that the second method specifically asks vgs to not print the heading and to only print the VG name field. The name is (on my system) often printed with leading and trailing spaces, which is why the grep expression looks the way it does.



            read -p "Enter the name of the volume group to be created: " vgname
            while vgs --readonly "$vgname" > /dev/null 2>&1
            do
            read -p "Enter the name of the volume group to be created: " vgname
            if vgs --noheadings -o vg_name | grep -q "^ *$vgname *$"
            then
            printf "That VG name is already taken; try something elsen" >&2
            fi
            done





            share|improve this answer

























              up vote
              1
              down vote













              Here's two different ways to check for the existence of a VG:



              1. attempt to read the VG directly vgs --readonly "$vgname"; if that command succeeds, the VG already exists.

              2. If the vgname is listed in the output from vgs, the VG already exists.

              Note that the second method specifically asks vgs to not print the heading and to only print the VG name field. The name is (on my system) often printed with leading and trailing spaces, which is why the grep expression looks the way it does.



              read -p "Enter the name of the volume group to be created: " vgname
              while vgs --readonly "$vgname" > /dev/null 2>&1
              do
              read -p "Enter the name of the volume group to be created: " vgname
              if vgs --noheadings -o vg_name | grep -q "^ *$vgname *$"
              then
              printf "That VG name is already taken; try something elsen" >&2
              fi
              done





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                Here's two different ways to check for the existence of a VG:



                1. attempt to read the VG directly vgs --readonly "$vgname"; if that command succeeds, the VG already exists.

                2. If the vgname is listed in the output from vgs, the VG already exists.

                Note that the second method specifically asks vgs to not print the heading and to only print the VG name field. The name is (on my system) often printed with leading and trailing spaces, which is why the grep expression looks the way it does.



                read -p "Enter the name of the volume group to be created: " vgname
                while vgs --readonly "$vgname" > /dev/null 2>&1
                do
                read -p "Enter the name of the volume group to be created: " vgname
                if vgs --noheadings -o vg_name | grep -q "^ *$vgname *$"
                then
                printf "That VG name is already taken; try something elsen" >&2
                fi
                done





                share|improve this answer













                Here's two different ways to check for the existence of a VG:



                1. attempt to read the VG directly vgs --readonly "$vgname"; if that command succeeds, the VG already exists.

                2. If the vgname is listed in the output from vgs, the VG already exists.

                Note that the second method specifically asks vgs to not print the heading and to only print the VG name field. The name is (on my system) often printed with leading and trailing spaces, which is why the grep expression looks the way it does.



                read -p "Enter the name of the volume group to be created: " vgname
                while vgs --readonly "$vgname" > /dev/null 2>&1
                do
                read -p "Enter the name of the volume group to be created: " vgname
                if vgs --noheadings -o vg_name | grep -q "^ *$vgname *$"
                then
                printf "That VG name is already taken; try something elsen" >&2
                fi
                done






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 3 at 15:15









                Jeff Schaller

                30.8k846104




                30.8k846104






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453246%2floop-to-keep-asking-for-the-value-until-user-enters-unique-value%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?