593

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
Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
Strawberry
  • 66,024
  • 56
  • 149
  • 197
  • They both work for me. Did you specify a shell in the *shebang* line? – Jason McCreary Nov 06 '10 at 02:02
  • 1
    @Jason I actually forgot the shebang line, but I tested it with it just now and it didn't work. – Strawberry Nov 06 '10 at 02:13
  • 27
    `>` is output redirection in sh/bash. Now you probably have a file named `1`. – mark4o Nov 06 '10 at 02:14
  • For information on Bash comparisons, please see my answers to previous questions [here](http://stackoverflow.com/questions/2600281/what-is-the-difference-between-operator-and-in-bash/2601583#2601583) and [here](http://stackoverflow.com/questions/3869072/test-for-non-zero-length-string-in-bash-n-var-or-var/3870055#3870055). For additional information regarding brackets, double brackets and double parentheses, see my answer [here](http://stackoverflow.com/questions/2188199/bash-double-or-single-bracket-parentheses-curly-braces/2188369#2188369). – Dennis Williamson Nov 06 '10 at 04:17
  • It doesn't work cause you mistype the compraison: use "$#" == "0" or $# -eq 0 – ETech Sep 18 '16 at 06:32

8 Answers8

1031

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))
 ...
JohannesM
  • 213
  • 2
  • 14
Coding District
  • 11,901
  • 4
  • 26
  • 30
  • 2
    I'm not sure if it's different in another shell but in bash, if you wish to use > or <, you have to put them in double parenthesis like so: (("$a" < "$b")) – Coding District Nov 06 '10 at 02:13
  • 1
    @Doug: It's not that unix doesn't use them, it's that bash and all the other shells I know use them for input/output redirection. – Cascabel Nov 06 '10 at 02:28
58

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.

jbremnant
  • 894
  • 6
  • 6
  • What is the option for greater than? Where's the official documentation for tihs? – Strawberry Nov 06 '10 at 02:07
  • 3
    Should be `-gt` for greater than. Or just replace it all with `[ $# -ne 1 ]`. – mark4o Nov 06 '10 at 02:08
  • Greater than is -gt, see my answer. The official documentation is in man test as jbremnant has pointed out. – Coding District Nov 06 '10 at 02:09
  • Just as an fyi for others, depending on your shell, most support == (now a days) as an comparison operator, however most of the time it is the same as the = operator. I would imagine that if you are using -gt and -ne for comparisons that it would be better practice to use -eq for == or = to keep your scripting style consistent. Happy Bashing :) – jkdba Nov 03 '15 at 13:58
39

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.

luca76
  • 813
  • 1
  • 10
  • 20
  • 2
    $# returns the number of arguments passed to the script. Useful for checking correct usage. It doesn't seem like the asker is using exit codes. – Centimane Aug 19 '15 at 11:53
31

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

if [[ $OUTMERGE == *"fatal"* ]] || [[ $OUTMERGE == *"Aborting"* ]]
  then
fi
TechNikh
  • 541
  • 5
  • 4
14

If a bash script

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

fi

exit
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Peprah David
  • 141
  • 1
  • 2
8

have you tried something like this:

if [ $# -eq 0 ] || [ $# -gt 1 ] 
then
 echo "$#"
fi
John Boker
  • 82,559
  • 17
  • 97
  • 130
6

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}
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • in bash you can do something like: `[[ -n $A ]] && echo 1 || echo 2` – premek.v Jan 16 '17 at 01:19
  • @premek.v thanks for the comment. My answer was very badly explained, I just edited it in the hope that it sheds more light to the topic. – fedorqui Jan 16 '17 at 09:57
1

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
Ravi Tyagi
  • 157
  • 2
  • 5