简体   繁体   中英

Or condition in Shell script in Solaris

When I run this code in Solaris bash:

a=false;
b=false;

if [ $a -o $b ] ; then
    echo "_True"
else
    echo "_False"
fi

result:

_True

Shouldn't the output of the script be false?

If I modify the script to something like this:

a=false;
b=false;

if [ $a = true -o $b = true ] ; then
    echo "_True"
else
    echo "_False"
fi

result:

_False

but this doesn't feel like a good coding practice to write "$a = true".

Please, can i know what is the issue here? and what exactly is the solution?

The [ command treats all non-empty strings as true. So use an empty string to represent false, rather than the string "false" . And remember to quote the variables.

a=
b=

if [ "$a" -o "$b" ]; then
    echo "_True"
else
    echo "_False"
fi

Another option is to use the true and false commands, rather than [ .

a=false
b=false
if $a || $b; then
    echo "_True"
else
    echo "_False"
fi

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