[: 2016-02-29: integer expression expected

Hi All,

Seeking for your assistance on my error encountered below. I think there's no problem on my script but i'm encountering integer expression expected. i already put double quote on my variable but still the same error i encountered.

#!/bin/sh

TASKDATE=2016-02-10
VSQL_F="connect"

dProcDate=`${VSQL_F} <<-EOF
SELECT LAST_DAY('$TASKDATE') AS dProcDate FROM DUAL;
EOF`

dCurrent=`${VSQL_F} <<-EOF
SELECT LAST_DAY(SYSDATE) AS dCurrent FROM DUAL;
EOF`

dProcDate2=`${VSQL_F} <<-EOF
SELECT LAST_DAY(ADD_MONTHS(SYSDATE,-1)) AS dProcDate FROM DUAL;
EOF`

if [ "$dProcDate" -lt "$dCurrent" ]; then
$dProcDate
else
$dProcDate2
fi


echo $TASKDATE
echo $dProcDate
echo $dCurrent
echo $dProcDate2
Error encountered:
jpv2.sh: line 18: [: 2016-02-29: integer expression expected
jpv2.sh: line 21: 2016-04-30: command not found
2016-02-10
2016-02-29
2016-05-31
2016-04-30

please advise,

Convert your date to seconds and then compare.

dProcDate="2016-02-29"
dCurrent="2016-05-31"

dProcDate_sec=$(date -d"$dProcDate" '+%s')
dCurrent_sec=$(date -d"$dCurrent" '+%s')

if [ $dProcDate_sec -le $dCurrent_sec ]; then
echo $dProcDate
else
echo $dProcDate2
fi
1 Like

oks will try

---------- Post updated at 04:10 PM ---------- Previous update was at 03:59 PM ----------

thanks sir pravin27. how come it's working. why do i need to convert it to sec?

Thanks,

You need convert it to seconds so you can use comparison operator in condition. Date format (2016-02-28) is not an integer , that's why script was throwing an error.

Since they're YYYY-MM-DD strings, you could also just compare them as strings.

if [ "$STRING1" "<" "$STRING2" ]
then
...
fi

You must put quotes around < > etc. or they will be taken as redirection operators.

That will work with some shells (and with some versions of the stand-alone

test

/ [ utilities), but the only string comparison operators defined for test in the standards are = and != .

We know that the submitter is using /bin/sh , but that isn't enough to know what operating system is being used nor what shell has been designated as /bin/sh on the submitter's system.

1 Like

For string comparison I prefer the [[ compound ]], supported by ksh/bash/zsh, and no fancy quoting:

if [[ $dProcDate < $dCurrent ]]
then
  echo older
fi