Use of expr to calc differenc ein to epoch values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use of expr to calc differenc ein to epoch values
# 1  
Old 05-01-2018
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;

Code:
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 ]]'

Code:
#!/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

Last edited by Scrutinizer; 05-02-2018 at 12:33 AM.. Reason: Additional code tags
# 2  
Old 05-01-2018
should use the && operator
Code:
if [[ ${DATE_DIFF} -gt 4000 && ${DATE_DIFF} -lt 6000 ]]

# 3  
Old 05-01-2018
Thank you!

any idea why the expr is causing an issue?

Code:
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}`


Last edited by Scrutinizer; 05-02-2018 at 12:32 AM.. Reason: code tags
# 4  
Old 05-01-2018
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:
Code:
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?
# 5  
Old 05-02-2018
It frequently helps to run a shell script with the -x option which immediately had shown the problem.
# 6  
Old 05-02-2018
Thanks all for the help,

I tried re-arranging the expression:

Code:
#!/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:

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


Any ideas on what to do here?


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-02-2018 at 06:40 AM.. Reason: Added CODE tags.
# 7  
Old 05-02-2018
Try inserting set -x into the script (and read man bash).
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

date calc

Hi, I need subtract two date values (which are in day of the year format) and the output would give the remaining days. using the command date +"%j" i would get today's 'day of the year' i.e., > date +"%j" 256 Next, i need to take input of a previous date in the format 09/05/2012 and then... (4 Replies)
Discussion started by: sam_bd
4 Replies

2. Shell Programming and Scripting

Calc max of a column

In C that was easy with a for and if. Iam trying to learn a litle more in bash. Example Ronaldo:5800 Figo:4000 Rafael:2321 Kaka:1230 I want the max of the $2 and the output will be: The max value is 5800 from Ronaldo. How can i do this in shell? Thanks for all, folks. (11 Replies)
Discussion started by: rafazz
11 Replies

3. Shell Programming and Scripting

test expr VS [ expr ]

What is the difference between test expr VS . For example : if test 5 -eq 6 echo "Wrong" and if echo "Wrong" bot will give the same output as Wrong. Now, what is the difference between these two? though they are producing the same result why we need two? Any answer will be... (2 Replies)
Discussion started by: ashok.g
2 Replies

4. UNIX for Dummies Questions & Answers

Differenc between ps -ef and top

First of all i really thankful to all those people who have created this site. unix.com rocks man!! I am bit confused about the two commands which are 'top' and 'ps -ef'.:confused: My requirement is that i want to monitor the CPU usage of Operating system and if the CPU usage is more than 90%... (2 Replies)
Discussion started by: Amey Joshi
2 Replies

5. Shell Programming and Scripting

Differenc between print and echo

can anyone explain me what is the difference between echo and print in shell programming? (3 Replies)
Discussion started by: chandhar
3 Replies
Login or Register to Ask a Question