Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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

  2. #2
    Just 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

  3. #3
    Linux 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.

  4. $spacer_open
    $spacer_close
  5. #4
    Just 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

  6. #5
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    Hi there,

    lomcevak's second suggestion is correct, you aren't setting $yorn correctly before entering the loop. The following should work:

    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
    Have fun.

  7. #6
    Just Joined!
    Join Date
    Mar 2009
    Posts
    7

    Exclamation

    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.

    Code:
     read yorn
    at
    Code:
     read $yorn
    and other change is as per my requirment my condition should be

    Code:
    while  [[ "$yorn" != "yes"  ||   "$yorn" != "no" ]]
    insted of

    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

  8. #7
    Linux Newbie Ziplock's Avatar
    Join Date
    Jan 2009
    Location
    Adelaide
    Posts
    169
    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.

  9. #8
    Linux 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •