Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, 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).... ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2012
    Posts
    58

    If command with && and ||


    Hi,

    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
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    hi,

    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

    try:
    Code:
    if test a -eq 1 && test b -eq 1 || test c -eq 1 || test d -eq 1
    then
       : do something
    fi

  3. #3
    Linux User jkwilborn's Avatar
    Join Date
    Nov 2012
    Posts
    285
    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.

    Jack

  4. $spacer_open
    $spacer_close

Posting Permissions

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