2

I'm relatively new with bash (programming in general) and have been self taught for the most part, but I have a while loop that is only supposed to exit if ALL the conditions are met.

while  [[ "$var1" != "$var1_tgt" ]] && [[ "var2" != "$var2_tgt" ]] && [[ "$var3" != "$var3_tgt" ]] && [[ "$var4" != "$var4_tgt" ]]; do LOOP...; done

The problem I seem to be having (I think) is in the evaluation of the loop. When I run the script it will break if only one of the conditions is met, but I want the loop to break only when ALL conditions have been satisfied. That is, when the var# equals the var#_tgt for ALL var's, only then should the loop break. As far as I know using "&&" should mean that all conditions must be met before the loop will exit, but the fact that it's not working means I am missing something. I appreciate any help figuring this out.

1
  • Can you help us understand what this script is actually doing? For example, you presumably start off with $var2_tgt matching $var, and $var2_tgt matching $var2, then as you progress through the loop one or more of the variables will be changed so they no longer match the target. And if they fail to match, you break out of the loop. Have I got that right? If so, can you provide us with example code that sets variables so we can test how things run? Commented Aug 25, 2015 at 4:11

1 Answer 1

1

You need to change the loop to:

while  [[ "$var1" != "$var1_tgt" ]] || [[ "var2" != "$var2_tgt" ]] || [[ "$var3" != "$var3_tgt" ]] || [[ "$var4" != "$var4_tgt" ]]; do LOOP...; done

Since you want to continue the loop if any of the variables are not yet at their target.

Otherwise, as soon as one of the variables reaches their target, one of the conditions will fail and the loop will break.

Sign up to request clarification or add additional context in comments.

5 Comments

I inferred that he wanted to continue the loop as long as and of the variables were still at their target. As soon as a variable is no longer on target, the loop should exit.
If I understand what JollyRoger is confused about, this is the correct answer. My take is that he's looking at the while clause as the exit condition for the loop, but it's actually the don't-exit condition. Thus, he's got "and" vs "or" backward.
I answered based on "when the var# equals the var#_tgt for ALL var's, only then should the loop break", which I think I've read correctly?
Sorry for the confusion... the variables initially start out off-target and over time will reach their target, but not all at the same time. I want the loop to exit only after all variables equal their respective targets and not when only one reaches is respective target. I hope that helps clarify. If not I will do my best to help clarify some more. Thanks for the input!
I think machaelcurtis may have answered the question. His reasoning sounds like what I am looking for but will have to test it to make sure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.