LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 12-10-2005, 12:20 PM   #1
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Rep: Reputation: 15
shell script - while loop with multiple conditions


Hi,

I'm sure this is possible as this is a very basic programming technique.
What I'm after is a while loop with multiple conditions - or (||) between the conditions.
Something like this:

while [ "$value" != "val1" || "$value" != "val2" || "$value" != "val3" ]

Obviously the above format doesn't work and I can't find one that will.

Thanks.
 
Old 12-10-2005, 12:53 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
while [[ "$value" != "val1" || "$value" != "val2" || "$value" != "val3" ]]
do
  <your code goes here>
done
The above will do what you want, you need to use double square brackets instead of single brackets.

Are you aware that using OR's (||) and NOT's (!=) like this in a while loop could cause some problems getting out of the loop? An exit or break is needed.

Hope this helps.

Last edited by druuna; 12-10-2005 at 01:09 PM.
 
Old 12-10-2005, 12:59 PM   #3
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Original Poster
Rep: Reputation: 15
thanks. that helps a lot.

i haven't tried it yet but I'm not aware of "problems" in getting out of the loop if I'll do it this way.
Will the loop continue even when none of the conditions is true? Is there a different way I can do it to avoid the problems?
 
Old 12-10-2005, 01:08 PM   #4
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Original Poster
Rep: Reputation: 15
I tried it an you are right.
Even though none of the conditions is true the loop kept going.

Any way to overcome this?
 
Old 12-10-2005, 01:09 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi again,

Once you enter the loop, you cannot leave it if you depend on the content of the value variable (the while statement will always be true). This isn't a bad thing per se, it depends on what you want to do.

Without knowing what it is you are trying to do it's kinda hard to give advise on how to proceed. And like I stated before, it could be that this is the correct sollution to your problem.

Using break will get you out of this the loop.
 
Old 12-10-2005, 01:15 PM   #6
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Original Poster
Rep: Reputation: 15
Yes, break will do the trick.
What I'm trying to do is to read a value from the user but I want to restrict him to 3 values only.
So inside the loop i have the 3 values as i stated beore and inside the loop i have the read command
(read value).

do u think break is my only option?
 
Old 12-10-2005, 01:46 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
I'm still not sure what it is you try to do:

- Is the user limited to 3 attempts (as in: 3 guesses to get the number between 0 and 10)?

If this is the case you could use a counter:

Code:
#!/bin/bash

counter=0

while [ $counter -lt 3 ]
do
  # other code goes here
  echo $counter
  # other code ends here

  let counter=counter+1
done
- Is the user limited to 3 input values (as in: give 3 numbers)?

Using read is an option, just call it 3 times:

Code:
#/bin/bash
read val1
read val2
read val3

echo $val1
echo $val2
echo $val3
If the above is not what you want to do: try explaining again

Hope this helps.
 
Old 12-10-2005, 02:00 PM   #8
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
I think what the OP is trying to do, is limit the user input to 3 "good" values.
ie: after the loop has exited value should be either val1, val2, or val3.

Did I get that right?

If so, use && instead of ||.
 
Old 12-10-2005, 02:17 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ntubski:

If you are correct then == should be used instead of != and || should stay.......

One value can not have three different values at the same time (arrays not included).
 
Old 12-10-2005, 02:41 PM   #10
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Original Poster
Rep: Reputation: 15
almost there.

the user need to enter a value but he has to choose one out of three.
My loop will ensure that if he hasn't entered one of those three then he will have to try again and again until he will enter one of those three.


while [ "$value" != "val1" || "$value" != "val2" || "$value" != "val3" ]
do
read value
done
 
Old 12-10-2005, 03:26 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Code:
while [[ "$value" != "val1" && "$value" != "val2" && "$value" != "val3" ]]
do
#you might add a prompt here 
read value
done
@drunna

I think I didn't explain myself clearly enough, but anyway I meant what ronsha is asking for.

Maybe I should of said after loop exits value can be val1 XOR val2 XOR val3
 
1 members found this post helpful.
Old 12-10-2005, 03:31 PM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ntubski

I indeed misunderstood. The above code works nicely.

@ronsha

I also think that ntubski's code does what you want.
 
Old 12-10-2005, 03:55 PM   #13
ronsha
Member
 
Registered: Oct 2005
Posts: 30

Original Poster
Rep: Reputation: 15
This indeed solved my problem.

Thanks everyone. I was so focused on syntax that I completley forgot to check my logic (&& instead of ||).

Thanks everyone for your help.
 
Old 12-10-2005, 04:08 PM   #14
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
You can also use select, if the user has to choose between val1, val2 and val3
Code:
PS3="Choose your value : "
select value in val1 val2 val3; do
    case $value in
        val1) echo val1 selected; break;;
        val2) echo val2 selected; break;;
        val3) echo val3 selected; break;;
    esac
done
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH Shell script : copying a file to multiple folder zamri Programming 14 04-29-2008 10:27 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 08:26 PM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM
Adding multiple user shell script plexus Programming 2 06-19-2004 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration