How to do a logical OR operation for integer comparison in shell scripting? How to do a logical OR operation for integer comparison in shell scripting? unix unix

How to do a logical OR operation for integer comparison in shell scripting?


This should work:

#!/bin/bashif [ "$#" -eq 0 ] || [ "$#" -gt 1 ] ; then    echo "hello"fi

I'm not sure if this is different in other shells but if you wish to use <, >, you need to put them inside double parenthesis like so:

if (("$#" > 1)) ...


This code works for me:

#!/bin/shargc=$#echo $argcif [ $argc -eq 0 -o $argc -eq 1 ]; then  echo "foo"else  echo "bar"fi

I don't think sh supports "==". Use "=" to compare strings and -eq to compare ints.

man test

for more details.


If you are using the bash exit code status $? as variable, it's better to do this:

if [ $? -eq 4 -o $? -eq 8 ] ; then     echo "..."fi

Because if you do:

if [ $? -eq 4 ] || [ $? -eq 8 ] ; then  

The left part of the OR alters the $? variable, so the right part of the OR doesn't have the original $? value.