Use of expr to calc differenc ein to epoch values

Hi folks,

I have a script where i am trying to calc the difference between two epoch dates and then have a condition based on the resultant value.

When I run the script it keeps complaining;

expr: syntax error
./testdiff.sh: line 11: syntax error in conditional expression
./testdiff.sh: line 11: syntax error near `-a'
./testdiff.sh: line 11: `if [[ ${DATE_DIFF} -gt 4000 -a ${DATE_DIFF} -lt 6000 ]]'
#!/bin/bash

export CHECK_DATE_1=`date +%s`
export FILE1=/tmp/longrunmon.out

export DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

while IFS=, read INSTANCE SESS_COUNT INSERT_TIME
do

if [[ ${DATE_DIFF} -gt 4000 -a ${DATE_DIFF} -lt 6000 ]]
then
echo Critical Alert: $DATE_DIFF

elif [[ ${DATE_DIFF} -lt 600 ]]
then
echo Major Alert: $DATE_DIFF

#else
#echo not less than 600
#echo instance: $INSTANCE
#echo session_count: $SESS_COUNT
#echo date_diff: $DATE_DIFF
#echo insert_time: $INSERT_TIME
#echo check_date: $CHECK_DATE_1

fi
done < ${FILE1}

The content of /tmp/longrunmon.out

is;

testinst,10,1525186184

Any help much appreciated.

jd

should use the && operator

if [[ ${DATE_DIFF} -gt 4000 && ${DATE_DIFF} -lt 6000 ]] 

Thank you!

any idea why the expr is causing an issue?

lit-dbracm01-p002:/home/oracle>./testdiff.sh
expr: syntax error
Major Alert:
Major Alert:
lit-dbracm01-p002:/home/oracle>cat testdiff.sh
#!/bin/bash

export CHECK_DATE_1=`date +%s`
export FILE1=/tmp/longrunmon.out

export DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

At the time that you define DATE_DIFF , INSERT_TIME has not yet been set. So DATE_DIFF , if your script were run now would provide an assignment similar to:

DATE_DIFF=`expr 1525226185 - `

which gets a syntax error when running expr because - is a binary operator and there is no second operand.

Maybe you meant to create a function named DATE_DIFF instead of a variable named DATE_DIFF ?

It frequently helps to run a shell script with the -x option which immediately had shown the problem.

Thanks all for the help,

I tried re-arranging the expression:

#!/bin/bash

export CHECK_DATE_1=`date +%s`
export FILE1=/tmp/longrunmon.out

#export DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

while IFS=, read INSTANCE SESS_COUNT INSERT_TIME

do

export DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

if [[ ${DATE_DIFF} -gt 4000 && ${DATE_DIFF} -lt 6000 ]]
then
echo Critical Alert: $DATE_DIFF

Still Im getting some syntax error:

lit-dbracm01-p002:/home/oracle>./testdiff.sh -x
expr: syntax error
Major Alert:

Any ideas on what to do here?

Try inserting set -x into the script (and read man bash ).

For some reason the INSERT_TIME value is not being picked up before the rest of the script.

while IFS=, read -r INSTANCE SESS_COUNT INSERT_TIME

do

DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

if [[ ${DATE_DIFF} -gt 69000 && ${DATE_DIFF} -lt 100000 ]]
then
echo Critical Alert: $DATE_DIFF

...

---------- Post updated at 11:56 AM ---------- Previous update was at 11:08 AM ----------

Hi yes, I did set -x

#!/bin/bash  -x 

The out put is now:

++ date +%s
+ CHECK_DATE_1=1525258379
+ FILE1=/tmp/longrunmon.out
+ IFS=,
+ read -r INSTANCE SESS_COUNT INSERT_TIME
++ expr 1525258379 -
expr: syntax error
+ DATE_DIFF=

For some reason the INSERT_TIME is not being read in this script:

#!/bin/bash  -x

CHECK_DATE_1=`date +%s`
FILE1=/tmp/longrunmon.out

while IFS=, read -r INSTANCE SESS_COUNT INSERT_TIME

DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

#echo instance: $INSTANCE
#echo session_count: $SESS_COUNT
#echo insert_time: $INSERT_TIME
#echo date_diff: $DATE_DIFF


do

if [[ ${DATE_DIFF} -gt 69000 && ${DATE_DIFF} -lt 100000 ]]
then
echo Critical Alert: $DATE_DIFF

elif [[ ${DATE_DIFF} -lt 600 ]]
then
echo Major Alert: $DATE_DIFF

fi

done < ${FILE1}

Show the (representatively curtailed) contents of /tmp/longrunmon.out .

lit-dbracm01-p002:/home/oracle>cat /tmp/longrunmon.out

testinst,10,1525186184

Is that an empty first line in your input? (see how IMPORTANT correct tagging is?)

oh yes, you're right!
thank you so much :slight_smile:

---------- Post updated at 04:08 PM ---------- Previous update was at 01:21 PM ----------

Actually I have one more bug:

Any idea why we see the same DIFF_DATE syntax error in expr for this section:

while IFS=, read -r INSTANCE SESS_COUNT INSERT_TIME

DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

do

if  [[ ${SESS_COUNT} -eq 0 ]]
then
echo Send Normal Alert

elif [[ ${DATE_DIFF} -gt 300 && ${DATE_DIFF} -lt 600 ]]
then
echo Critical Alert: ${DATE_DIFF}

elif [[ ${DATE_DIFF} -gt 600 ]]
then
echo Major Alert: ${DATE_DIFF} ${INSTANCE}

fi
done < ${FILE1}
+ IFS=,
+ read -r INSTANCE SESS_COUNT INSERT_TIME
++ expr 1525273451 -
expr: syntax error
+ DATE_DIFF=

It seems to another read of the file at the end of the script even though there are no extra lines before the data or after the data in the file:


lit-dbracm01-p002:/home/oracle>cat /tmp/longrunmon.out
testinst,0,1525186184
testinst-2,0,1525186184

please used code tags as noted in post11.
It looks like you still have a misconfigured file you're trying to read...

Im not sure what you mean by correct tagging.

My files seems good enough. no empty line anywhere.

$:/home/oracle>cat /tmp/longrunmon.out
testinst,10,1525186184
testinst-2,10,1525186184
$:/home/oracle>

please use code tags.

If the numbers involved are simple integers (which seconds from the Epoch should be) then why not do something smarter, such as replacing

DATE_DIFF=`expr ${CHECK_DATE_1} - ${INSERT_TIME}`

with

((DATE_DIFF=${CHECK_DATE_1}-${INSERT_TIME}))

You can probably use a let statement or other shell coding and these save the effort of creating a subshell to run expr in.

It might not solve your problem, but it could be a better practice, especially if you are reading large files and doing lots of calculations.

Robin

is there an exit i should be using somewhere?

It seems to be trying to use another empty line but I cant see one anywhere

Can you post the output from this little script:-

#!/bin/bash

while read line
do
   printf "I have read \"${line}\" from the file.\n\n"
done < /tmp/longrunmon.out

Please carefully wrap it in CODE tags so we can be clear what the output is.

Thanks, in advance,
Robin

> 
server:/home/oracle>while read line
> do
>    printf "I have read \"${line}\" from the file.\n\n"
> done < /tmp/longrunmon.out

output

I have read "testinst2,10,1525186184" from the file.

I have read "testinst,10,1525186184" from the file.

server:/home/oracle>

Maybe its that empty lines that has appeared? not quite sure how they appear?

are there any ^M-s in the file you're reading from?
do cat -vet /tmp/longrunmon.out and post the output (in code tags).