Results 1 to 3 of 3
Thread: If command with && and ||
Enjoy an ad free experience by logging in. Not a member yet? Register.
- Join Date
- Apr 2012
If command with && and ||
I would like to write an if command in bash but I have some syntax problems. I would like to write IF (a=1) and (b=1) or (c=1) or (d=1)....
if [ a=1] && [b=1] || [c=1] || [d=1]
but it does not work.
Could you please help me???
Thank you in advance.
- Join Date
- Nov 2012
it does not work
what are you trying to do ?
what's the exact error message?
- spaces are meaningful
- = is for lexical comparision
if test a -eq 1 && test b -eq 1 || test c -eq 1 || test d -eq 1 then : do something fi
If it's syntactic then this won't help. What I'd like to do is point out that the way you have it coded may not be what you want or how the machine is interpreting the line.
Keep in mind that there are priorities in most logical and mathematical equations and that you force or ensure the correct interpretation by using parenthesis. You might try that to improve readability and actual interpretation.
For instance the term
if a & b or c
may be taken as a result of oring b and c, then anding it with a.
a + b * c will be evaluated as b*c then will add a to that product. Simply because multiplication has a higher priority. to get a + b * c you have to force the (a + b) * c to get a different result. Of course you may be lucky...
I'm just suggesting you use parens or brackets or whatever the interpreter uses to properly do what you want AND to let those reading it to understand that it doing something specific...
Just a thought.