1753862 Members
7749 Online
108809 Solutions
New Discussion юеВ

Re: while loop

 
SOLVED
Go to solution
Donald C Nelson
Frequent Advisor

while loop

I am using this for loop to create volume groups and it works just fine, but if an admin types something else besides 4,8,16 or 32, it will blow up. How can I setup a while loop to make sure they only input 4,8,16,32



echo -n "Please enter the number of physcial extents for the vgcreate (4,8,16, etc): "
read pe
for i in 2 3 4 5 6
do
echo "...Creating volume group vg0$i..."
vgcreate -s $pe $i `cat $target_dir/vg0$i.out`
done
5 REPLIES 5
RAC_1
Honored Contributor

Re: while loop

read pe
after this add
if [ ${pe} -eq "4" -o ${pe} -eq "8" -o ${pe} -eq "16" -o ${pe} -eq "32" ];then
for i in 2 3 4 5 6
do
echo "...Creating volume group vg0$i..."
vgcreate -s $pe $i `cat $target_dir/vg0$i.out`
done
else
echo "bad Choice"
exit 1
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: while loop

There are a number of ways to attack this but the case statement is an easy approach:

read pe
case "${pe}" in
4|8|16|32);;
*) echo "Invalid input" >&2
exit 255;;
esac
for i ....
If it ain't broke, I can fix that.
Donald C Nelson
Frequent Advisor

Re: while loop

The case statement will work for me. Thanks
Clay, but I need for it to loop back to another response. Is there a command I can put in after the "Invalid Choice"
A. Clay Stephenson
Acclaimed Contributor

Re: while loop

Does a hog like slop?

OK=0
until [[ ${OK} -ne 0 ]]
do
read pe
case "${pe}" in
4|8|16|32) OK=1
;;
*) echo "Invalid input" >&2
;;
esac
done

for i ....
If it ain't broke, I can fix that.
Donald C Nelson
Frequent Advisor

Re: while loop

Thanks Clay, right when I sent the reply, I found my answer.