简体   繁体   中英

Shell Script logical operator for conditions

 $i="500,600"
 $j="600"

 if[$i -ne $j]; then
    #some line
 else
    #some line
 fi

This if condition is not going inside.

this if condition fails. else is pass.

how is this possible

can someone help me on this

The trick is to consider the "[" sign as a command (in fact it is one indeed), whose last argument must be a "]". So you must ensure that there is a space after [ and all of its arguments go to the proper place. In your case:

if [ "$i" -ne "$j" ]
 then
     # some code
 else
     # some code
fi

Since [ is a command, you might want to omit the if structure and use logical operators, taking advantage of lazy evaluation. The following means the same:

[ "$i" -ne "$j" ] && {
    echo "hello" ;
    echo "world" ;
} || { 
    echo "bye bye" ;
    echo "world" ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM