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

BashUnixIf StatementSh

Bash Problem Overview


I am trying to do a simple condition check, but it doesn't seem to work.

If $# is equal to 0 or is greater than 1 then say hello.

I have tried the following syntax with no success:

if [ "$#" == 0 -o "$#" > 1 ] ; then
 echo "hello"
fi

if [ "$#" == 0 ] || [ "$#" > 1 ] ; then
 echo "hello"
fi

Bash Solutions


Solution 1 - Bash

This should work:

#!/bin/bash

if [ "$#" -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))
 ...

Solution 2 - Bash

This code works for me:

#!/bin/sh

argc=$#
echo $argc
if [ $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.

Solution 3 - Bash

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.

Solution 4 - Bash

Sometimes you need to use double brackets, otherwise you get an error like too many arguments

if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]]
  then
fi

Solution 5 - Bash

If a bash script

If [[ $input -gt number  ||  $input  -lt number  ]]
then 
    echo .........
else
    echo .........

fi

exit

Solution 6 - Bash

have you tried something like this:

if [ $# -eq 0 ] || [ $# -gt 1 ] 
then
 echo "$#"
fi

Solution 7 - Bash

From Bash Reference Manual → 3.4.2 Special Parameters

> #
($#) Expands to the number of positional parameters in decimal.

Therefore, $# will always be either 0 or a bigger integer.

So if you want to do something whenever $# is either 0 or bigger than 1, you just have to check if $# is or is not 1:

[ $# -eq 1 ] && echo "1 positional param" || echo "0 or more than 1"

This uses the syntax:

[ condition ] && {things if true} || {things if false}

Solution 8 - Bash

And in Bash

 line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
 vpid=`ps -ef|  grep wowzaDataSync | grep -v grep  | awk '{print $2}'`
 echo "-------->"${line1}
    if [ -z $line1 ] && [ ! -z $vpid ]
    then
            echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
            "Process Is Working Fine"
    else
            echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
            "Prcess Hanging Due To Exception With PID :"${pid}
   fi

OR in Bash

line1=`tail -3 /opt/Scripts/wowzaDataSync.log | grep "AmazonHttpClient" | head -1`
vpid=`ps -ef|  grep wowzaDataSync | grep -v grep  | awk '{print $2}'`
echo "-------->"${line1}
   if [ -z $line1 ] || [ ! -z $vpid ]
    then
            echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
            "Process Is Working Fine"
    else
            echo `date --date "NOW" +%Y-%m-%d` `date --date "NOW" +%H:%M:%S` :: 
            "Prcess Hanging Due To Exception With PID :"${pid}
  fi

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStrawberryView Question on Stackoverflow
Solution 1 - BashCoding DistrictView Answer on Stackoverflow
Solution 2 - BashjbremnantView Answer on Stackoverflow
Solution 3 - Bashluca76View Answer on Stackoverflow
Solution 4 - BashTechNikhView Answer on Stackoverflow
Solution 5 - BashPeprah DavidView Answer on Stackoverflow
Solution 6 - BashJohn BokerView Answer on Stackoverflow
Solution 7 - BashfedorquiView Answer on Stackoverflow
Solution 8 - BashRavi TyagiView Answer on Stackoverflow