Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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)....

    I tried:
    if [ a=1] && [b=1] || [c=1] || [d=1]
    but it does not work.
    Could you please help me???
    Thank you in advance.

  2. #2

    it does not work
    does not help much to tell what's wrong.

    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
       : do something

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


  4. $spacer_open

Posting Permissions

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