LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-30-2009, 06:42 AM   #1
ykc
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Rep: Reputation: 0
shell script , while loop


hi ,

i am trying a shell script, to wait until user enter's yes/no,
i am not getting out from while loop, what ever i enter including yes/no

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
 
Old 03-30-2009, 07:00 AM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 410

Rep: Reputation: 48
No shell righ now but here is how i'd do it


Code:
until [ ${yorn} != "yes" ] || [ ${yorn} != "no" ]; do
 echo 'do u want cont.. [yes/no]'
 read yorn
 $yorn=`echo ${yorn}|tr '[A-Z]' '[a-z]'`
 echo "u say $yorn"
done

You'll need to check the syntax but you get the idea.
 
Old 03-30-2009, 07:11 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524

Rep: Reputation: 93
  • For testing in "if", "while" and so, use single [ and ] (you had double: [[ ]]).
  • Use && instead of ||. You want the loop to run if the user did not enter "yes" AND did not enter "no".
  • When && or ||, you need to enclose both sides of it in '[' and ']'.

Fixed script: (changed line in bold)
Code:
#!/bin/bash

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

Last edited by Hko; 03-30-2009 at 07:13 AM.
 
Old 03-30-2009, 07:30 AM   #4
ykc
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Hi

thanks eco,

I am very new to shell scripiting,
errors when i run code is
Code:
./dep_1.sh: line 1: [: !=: unary operator expected
./dep_1.sh: line 1: [: !=: unary operator expected
do u want cont.. [yes/no]
3
u say 3
& i am out from the loop,

i did some gusses to that code at line one but no use...

And thanks for using tr/translate, using tr looks good,
 
Old 03-30-2009, 07:41 AM   #5
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 410

Rep: Reputation: 48
As Hko said so well, we need double '[]' and we want '==' not '!='

So:

Code:
until [[ ${yorn} == "yes" ]] || [[ ${yorn} == "no" ]]; do

Last edited by eco; 03-30-2009 at 07:43 AM.
 
Old 03-30-2009, 07:50 AM   #6
ykc
LQ Newbie
 
Registered: Mar 2009
Posts: 4

Original Poster
Rep: Reputation: 0
Thumbs up

thnks Hko, it working
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script skipping a loop dnvikram Programming 2 01-23-2009 02:29 PM
Loop in Shell Script delamatrix Programming 4 07-24-2008 05:20 PM
optional exit from loop, shell script advanced Programming 2 07-17-2008 03:30 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM


All times are GMT -5. The time now is 05:19 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration