Loop to keep asking for the value until user enters unique value
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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
bash lvm
add a comment |Â
up vote
1
down vote
favorite
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
bash lvm
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
bash lvm
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
bash lvm
edited Jul 3 at 15:01
ilkkachu
47.3k668130
47.3k668130
asked Jul 3 at 14:56
Vivek Dabas
234
234
add a comment |Â
add a comment |Â
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.
pvs displays all the Phyical Volumes available in OS.
â Vivek Dabas
Jul 6 at 16:58
add a comment |Â
up vote
1
down vote
Here's two different ways to check for the existence of a VG:
- attempt to read the VG directly
vgs --readonly "$vgname"
; if that command succeeds, the VG already exists. - 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
add a comment |Â
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.
pvs displays all the Phyical Volumes available in OS.
â Vivek Dabas
Jul 6 at 16:58
add a comment |Â
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.
pvs displays all the Phyical Volumes available in OS.
â Vivek Dabas
Jul 6 at 16:58
add a comment |Â
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.
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.
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
add a comment |Â
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
add a comment |Â
up vote
1
down vote
Here's two different ways to check for the existence of a VG:
- attempt to read the VG directly
vgs --readonly "$vgname"
; if that command succeeds, the VG already exists. - 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
add a comment |Â
up vote
1
down vote
Here's two different ways to check for the existence of a VG:
- attempt to read the VG directly
vgs --readonly "$vgname"
; if that command succeeds, the VG already exists. - 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
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Here's two different ways to check for the existence of a VG:
- attempt to read the VG directly
vgs --readonly "$vgname"
; if that command succeeds, the VG already exists. - 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
Here's two different ways to check for the existence of a VG:
- attempt to read the VG directly
vgs --readonly "$vgname"
; if that command succeeds, the VG already exists. - 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
answered Jul 3 at 15:15
Jeff Schaller
30.8k846104
30.8k846104
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password