Bash - Greater or equals expression evaluation

Viewed 47

In my bash script I have this:

  if [ $AWFCNT -ge 1 ] ; then
   /usr/bin/mailx -s "$SUB" $TO < $AWFOUT
  fi

  if [ $DMSCNT -ge 1 ] ; then
   /usr/bin/mailx -s "$SUB" $TO < $DMSOUT
  fi

It gives me this error: [: -ge: unary operator expected

I tried different SO threads with similar situation and then I tried using double [[]] too but then the condition is not evaluated at all. Also I tried using () with >= but then I get this error: ((: >= 1 : syntax error: operand expected (error token is ">= 1 ")

What am I doing wrong here?

Here is the full script:

#!/bin/bash
PWD="/home/ajay/mydir/mydata/"
AWFJAR="$PWD/AWF.jar"
DMSJAR="$PWD/DMS.jar"
AWFOUT="/home/somedir/ContentQueueDataCountAWF.txt"
DMSOUT="/home/somedir/ContentQueueDataCountDMS.txt"
SUB='"Action Required - Content Queue count exceeded 500000"'
TO=someemail@domain.com
/usr/bin/java -jar $AWFJAR
sleep 10
/usr/bin/java -jar $DMSJAR
sleep 10
AWFCNT=`cat $AWFOUT | awk '{print $16}'`
DMSCNT=`cat $DMSOUT | awk '{print $16}'`
  if [ $AWFCNT -ge 1 ] ; then
   /usr/bin/mailx -s "$SUB" $TO < $AWFOUT
  fi

  if [ $DMSCNT -ge 1 ] ; then
   /usr/bin/mailx -s "$SUB" $TO < $DMSOUT
  fi

Here, I am trying to read a number from the first line of the text files and based on the condition I am trying to send an email.

1 Answers

Not an answer, more of a formatted comment:

$ VAR=""

$ [ $VAR -ge 1 ]
bash: [: -ge: unary operator expected

$ [ "$VAR" -ge 1 ]
bash: [: : integer expression expected

$ (($VAR >= 1)) && echo yes || echo no
bash: ((: >= 1: syntax error: operand expected (error token is ">= 1")
no

$ ((VAR >= 1)) && echo yes || echo no
no

Drop the $ for variables in an arithmetic expression.

Empty or unset variables get the default value zero.

Related