Results 1 to 8 of 8

Thread: bash: if santax help: [ X ] && [ Y ] || [ Z ]

  1. #1
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    4,305
    Distro
    Xubuntu 13.04 Raring Ringtail

    bash: if santax help: [ X ] && [ Y ] || [ Z ]

    How do i write this in sh/bash

    Javascript/php:
    Code:
    if ( 1 == 1 && ( 1==2 || 2==2 ) ) {
    
    }
    This is the closest i can get without breaking it:
    Code:
    if [ 1 -eq 1 ] && [ 1 -eq 2 ] || [ 2 -eq 2 ];then
    
    fi
    Last edited by pqwoerituytrueiwoq; April 29th, 2013 at 03:51 PM. Reason: Solved
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  2. #2
    Join Date
    Mar 2010
    Location
    India
    Beans
    8,175
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    Edit: Forget it, it's all wrong. The following posts explain why.. trying variations, may post corrected one later..

    Code:
    if [[ 1 -eq 1 && 1 -eq 2 || 2 -eq 2 ]];then
    
    fi
    works for me.

    For example :
    Code:
    a=2; b=3; c=4; if [[ $a -eq 2 && $b -eq 2 || $c -eq 4 ]]; then echo yes; fi
    ..change the declared values or test values and see the difference. It works as -
    IF (condition 1) AND (condition2 OR condition3), THEN go ahead..
    Last edited by varunendra; April 29th, 2013 at 11:30 AM. Reason: Forgot $ in variable calls, Thanks to schragge for the heads up!
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags
    Am I not replying you? Perhaps this is why.

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    There's a subtle difference between &&/|| and -a/-o in that the former short-curcuit, but the latter don't. See compound comparison in ABS.

    In this particular case, I'd use arithmetic evaluation
    Code:
    ((1==1 && 1==2 || 2==2))
    Also, using of parentheses to explicitly group subexpressions is allowed inside double-brackets and arithmetic expressions.

    Edit || has lower precedence than &&, so parentheses are needed. See spjackson's explanation below.
    Last edited by schragge; April 29th, 2013 at 12:00 PM.

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,119
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    The truth table for the original Javascript/php is:
    Code:
    TTT==>T
    TTF==>T
    TFT==>T
    TFF==>F
    FTT==>F
    FTF==>F
    FFT==>F
    FFF==>F
    However both of these suggestions:
    Code:
    if [[ 1 -eq 1 && 1 -eq 2 || 2 -eq 2 ]];then
    
    ((1==1 && 1==2 || 2==2))
    have the truth table
    Code:
    TTT==>T
    TTF==>T
    TFT==>T
    TFF==>F
    FTT==>T
    FTF==>F
    FFT==>T
    FFF==>F
    i.e. different in 2 cases.

    Just as in the original, you need a bracket;
    Code:
    if [[ 1 -eq 1 && ( 1 -eq 2 || 2 -eq 2 ) ]];then
    
    ((1==1 && (1==2 || 2==2)))

  5. #5
    Join Date
    Mar 2010
    Location
    India
    Beans
    8,175
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    Quote Originally Posted by spjackson View Post
    Just as in the original, you need a bracket;
    Code:
    if [[ 1 -eq 1 && ( 1 -eq 2 || 2 -eq 2 ) ]];then
    Verified this one ^^ with my example, changing all three variables and seems perfect!
    Varun
    Help others by marking threads as [SOLVED], if they are. (See how)
    Wireless Script | Use Code Tags
    Am I not replying you? Perhaps this is why.

  6. #6
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    4,305
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    Thanks everyone
    this is where i needed it
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    Two things about your script.

    1. It downloads kernel-related packages to current directory and then counts files to check if they were successfully downloaded. What if there already were previously downloaded older kernel packages in the folder?

    2. This
    Code:
    if [ "`uname -m`" == "x86_64" ];then
    arch="amd64"
    else
    arch="i386"
    fi
    probably could be replaced with
    Code:
    arch=`dpkg --print-architecture`
    and this
    Code:
    [ $(ls /boot | grep $VER_A | wc -l) -gt 3 ]
    with
    Code:
    [[ -d /lib/modules/$VER_A ]]
    Last edited by schragge; April 29th, 2013 at 07:56 PM.

  8. #8
    Join Date
    Jun 2009
    Location
    0:0:0:0:0:0:0:1
    Beans
    4,305
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: bash: if santax help: [ X ] && [ Y ] || [ Z ]

    If there are files present it will ask you if you want to delete them, if you say no wget will not replace them and will get the other files
    edit:
    opps now what you meant, thanks
    https://github.com/GM-Script-Writer-...criptGenerator
    Last edited by pqwoerituytrueiwoq; April 29th, 2013 at 08:22 PM.
    Laptop: ASUS A54C-NB91 (Storage: WD3200BEKT + MKNSSDCR60GB-DX); Desktop: Custom Build - Images included; rPi Server
    Putting your Networked Printer's scanner software to shame PHP Scanner Server
    I frequently edit my post when I have the last post

Bookmarks

Posting Permissions

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