Results 1 to 8 of 8
hi ,
i am trying a shell script, to wait untill user enter's y/n,
code :
yorn=' '
while [ $yorn == yes -o $yorn == no ]
do
echo ...
- 03-28-2009 #1Just Joined!
- Join Date
- Mar 2009
- Posts
- 7
shell script while loop
hi ,
i am trying a shell script, to wait untill user enter's y/n,
code :
yorn=' '
while [ $yorn == yes -o $yorn == no ]
do
echo 'do u want cont.. [yes/no]'
read
$yorn=$REPLY
echo 'u say '$yorn
done
each time it is giving different out put..,
./dep_1.sh: line 3: [: too many arguments
or some times no out put,,
can any one help me
thanks
- 03-28-2009 #2Just Joined!
- Join Date
- Mar 2009
- Posts
- 7
hi agian,
I am new to shell script, i am not understanding what mistake i did , any suggestions plz
- 03-28-2009 #3Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
First, when using the value of string variables you should enclose them with double quotes.
"$yorn" == yes instead of $yorn == yes
Next, your while condition is wrong. It's saying 'while value of yorn is yes or no'. Initially yorn is '' so the condition is false thus never entering the loop. Either change it to 'until value of yorn is yes or no' or 'while value of yorn is not yes and not no'.
Finally you'll have a problem with the line $yorn=$REPLY.
- 03-29-2009 #4Just Joined!
- Join Date
- Mar 2009
- Posts
- 7
Hi,
lomcevak u r fist suggestion is okey, when i try with that its not entering into while loop,
thats not much useful,
next i am changing value of yorn in line 6:
$yorn=$REPLY
next i am printing that ,there the value of yorn is changing
echo 'u say '$yorn
any other suggestions ??
thanks
ykc
- 03-30-2009 #5
Hi there,
lomcevak's second suggestion is correct, you aren't setting $yorn correctly before entering the loop. The following should work:
Have fun.Code:echo 'do u want cont.. [yes/no]' read yorn while [[ "$yorn" == "yes" || "$yorn" == "no" ]] do echo 'do u want cont.. [yes/no]' read $yorn echo 'u say '$yorn done
- 03-30-2009 #6Just Joined!
- Join Date
- Mar 2009
- Posts
- 7
hi agian,
thank you Ziplock. i tried your code,
once i entered into while loop i am not comming out from it, what ever i entered.., including 'yes/no' ,
and i did some changes to that code, since yorn value is not changing at next echo
But still problem is exist.
atCode:read yorn
and other change is as per my requirment my condition should beCode:read $yorn
insted ofCode:while [[ "$yorn" != "yes" || "$yorn" != "no" ]]
,Code:while[[ "$yorn" == "yes" || "$yorn" == "no" ]]
now my code is this,
Code:echo 'do u want cont.. [yes/no]' read yorn while [[ "$yorn" != "yes" || "$yorn" != "no" ]] do echo 'do u want cont.. [yes/no]' read yorn echo 'u say '$yorn done
- 03-30-2009 #7
Hi there,
Yeah, your code would loop if you got 'yes' or 'no'. Sorry about the other yorn instance - that was a typo on my part. Glad you got it working though.
- 03-30-2009 #8Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
ykc#, you script still shouldn't work because of the while statement:
while [[ "$yorn" != "yes" || "$yorn" != "no" ]]
It's saying while value of yorn not equal yes OR not equal no. This condition will always be true so you stay in the loop. Why it stays in the loop is because the '||' says only one condition has to be true which it is as yorn will always not equal yes or no.
The while command should be:
while [[ "$yorn" != "yes" && "$yorn" != "no" ]]
which says value of yorn not equal yes AND not equal no. In this case the '&&' says both conditions have to be true to stay in the loop.
Hope that helps.


Reply With Quote