bash problem when comparing integers

Hi,

When I compare currentfiledate to reportfiledate I get a "-bash: [: 20090220080101: integer expression expected " error message.

currentfiledate=20090220080101
reportfiledate=20090219231245

if [ $currentfiledate -gt $reportfiledate ] ; then
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
echo -ne "!! Running the load utility !! \n"
fi

Our computer has a 64 bit processor, so I know it can handle the value in the variable.

What am I missing ?

Thank you.

works fine under Solaris' "bash".
Does not work correctly under 'sh'.

Make sure you specify a desired shell interpreter in the first line of your script:

#!/bin/bash
currentfiledate=20090220080101
reportfiledate=20090219231245

if [ $currentfiledate -gt $reportfiledate ] ; then
   echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
   echo -ne "!! Running the load utility !! \n"
fi

Hi,

I had the statement in the program. Sorry, I did not paste it.

Here is more info about the bash version we're using :

>bash --version
GNU bash, version 2.05b.0(1)-release (powerpc-ibm-aix5.1.0.0)
Copyright (C) 2002 Free Software Foundation, Inc.

Thank you.

ok, try this:

#!/bin/bash
#set -x
typeset -i currentfiledate=20090220080101
typeset -i reportfiledate=20090219231245

if [ $currentfiledate -gt $reportfiledate ] ; then
     echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
     echo -ne "!! Running the load utility !! \n"
fi

No....

Same error message :

bash: [: 20090220080101: integer expression expected

hm.....
set the 'set -x' option and see what's being executed.
Also make sure there're no 'escape' characters in your script:

cat -vet myScriptFile.sh

Also, how exactly do you call/execute your script?

Here is the output of the 'set -x' command :

+ typeset -i currentfiledate=20090220080101
+ typeset -i reportfiledate=20090219231245
+ '[' 20090220080101 -gt 20090219231245 ']'
./roger_eod_ofac.cfg: line 6: [: 20090220080101: integer expression expected

Here is the output of the 'cat -vet' command :

#!/usr/local/bin/bash$
set -x$
typeset -i currentfiledate=20090220080101$
typeset -i reportfiledate=20090219231245$
$
if [ $currentfiledate -gt $reportfiledate ] ; then$
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"$
echo -ne "!! Running the load utility !! \n"$
fi$
$

I execute the program using the program name at the prompt 'roger_eod_ofac.cfg' also by preceding the program name with a period '. roger_eod_ofac.cfg'.

Thank you for helping...

Most shells won't hand integers that big.
In the case of a reversed date string a text compare should do.

#!/bin/ksh
currentfiledate="20090220080101"
reportfiledate="20090219231245"

if [ "${currentfiledate}" -gt "${reportfiledate}" ]
then
        echo "message"
fi

Hi,

Yes, the reversed date string compare works but the problem is that I have to add this logic to a bash script and the bash script will not handle "-gt" when comparing strings.

Thank you.

If there was a problem with the size of the integer, I should also have a problem changing their values, adding to them, But this code runs without error except when it is time to compare the variables.

#!/usr/local/bin/bash
currentfiledate=20090220080101
reportfiledate=20090219231245

currentfiledate=$(expr $currentfiledate + 1001)
reportfiledate=$(expr $reportfiledate + 1001)

echo $currentfiledate
echo $reportfiledate

if [ $currentfiledate -gt $reportfiledate ] ; then
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
echo -ne "!! Running the load utility !! \n"
fi

Results from 'echo':
20090220081102
20090219232246

The smallest values I can use, when comparing without error, are between 1 and 999999999.

Thanks.

If there was a problem with the size of the integer, I should also have a problem changing their values, adding to them, But this code runs without error except when it is time to compare the variables.

#!/usr/local/bin/bash
currentfiledate=20090220080101
reportfiledate=20090219231245

currentfiledate=$(expr $currentfiledate + 1001)
reportfiledate=$(expr $reportfiledate + 1001)

echo $currentfiledate
echo $reportfiledate

if [ $currentfiledate -gt $reportfiledate ] ; then
echo -ne "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! \n"
echo -ne "!! Running the load utility !! \n"
fi

Results from 'echo':
20090220081102
20090219232246

The smallest values I can use, when comparing without error, are between 1 and 999999999.

Thanks.

Try using the shell keyword [[ instead of the [ builtin:

[[ $currentfiledate -gt $reportfiledate ]]

Or better use the (( )) compound command:

(( "$currentfiledate" > "$reportfiledate" ))

If you're on Solaris, you may consider using the POSIX shell /usr/xpg4/bin/sh instead of this old version of bash,
notice that with that shell you should use the standard syntax: [ expression ].

Hi radoulov,

It works, I tried both ways with bash (I have no option on the shell selection) and they both worked.

Could you explain the difference between one set of square brackets vs 2 sets and how come I can use the ">" symbol as "greater than" in bash when using 2 sets of parentheses?

Thanks so much.

As far as the bash shell is concerned, you have:

  1. The [ and the test builtin commands. In this context (after the [ and the test command) parameter expansion, word splitting and pathname expansion are performed[1]. So beware of the following situation when using the [ and the test commands:
  • missing quotes
    -- unset or null values:
$ unset var
$ [ $var -eq 1 ]
bash: [: -eq: unary operator expected

-- values containing IFS characters:

$ var="a b"
$ [ $var = "a b" ]
bash: [: too many arguments

-- pathname expansion:

$ touch x
$ var=\*
$ set -x
$ [ $var = "*" ]
+ '[' x = '*' ']'
  • particular syntax for logical comparison:
$ [ value = value -a 1 -lt 2 ]&&echo true
true
  1. The [[ non standard (ksh93, zsh and bash) compound command. Parameter expansion is performed inside [].
    No word splitting and pathname expansion are performed[2]. Support more operators.
    Consider the following:
  • no need to quote unset variables or variables with null values or values containing IFS characters:
$ unset var
$ set -x              
$ [[ $var -eq value ]]
+ [[ '' -eq value ]]
$ var=a\ b
$ set -x
[[ $var == "a b" ]]
+ [[ a b == \a\ \b ]]
  • new versions of bash support new operators:
$ var=abc              
[[ $var =~ ^a ]]&&echo OK
OK
$ echo "${BASH_REMATCH[@]}"
a
  • readable operators for logical comparison:
$ [[ value == value && 2 < 1 ]]||echo false
false
  1. The compound command ((expression)). From the manual pages for bash[3]:
$ unset a
$ ((++a))&&echo $a # version >= 3
1

And as far as your particular problem is concerned it seems like a limitation of the old version of bash you're using,
your code works with recent versions of the bash shell.

[1] For more details see the manual pages for bash, the section Shell Builtin Commands and CONDITIONAL EXPRESSIONS.
[2] For more details see the manual pages for bash, the section Compound Commands. Tilde expansion, arithmetic expansion, command substitution, process substitution, and quote removal are also performed.
[3] For more information see:

man bash|less -p^ARITHMETIC\ EVALUATION

Hope this helps.

Wow....

Thank you very much radoulov...

[ ... ] is standard and portable; [[ ... ]] is not.

There are other differences, but one only time the double brackets are necessary is when you want to compare against a regular expression.