CurrentTime-4 hours

Hi,
Good Afternoon!
I am writing this script on "sh" and have Variables as below.

#Time in hours ex: 09
JobTime=`echo $StartTime |awk '{print $2}'|cut -f1 -d':'`
SystemHours=`date +%H`

How can go 4 hours back for each variable in a day?

Another Question?

JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
if [ "$SysDate" == "$JobRunDate" ] && [ "$JobStTime" -ge "$CurSysHrs" ] && [ [ "x3" >= "x${JobStat}" ] || [ "x0" != "x${JobStat}" ] || [ "x2" != "x${JobStat}" ] ] 
then
{
echo "$JobName,$StartTime,$EndTime,$JobStat" >>$Jobstatus
}
fi

I tried this

[ [ "x3" >= "x${JobStat}" || "x0" != "x${JobStat}" || "x2" != "x${JobStat}" ] ] and [ [ "x3" -ge "x${JobStat}" || "x0" -ne "x${JobStat}" || "x2" -ne "x${JobStat}" ] ]

also
Please help me in if condition with "||" Statement.

Don't know which flavor of unix OS you are on, but if you have gnu date (which linux has by default) , you can use:

date -d "4 hours ago"

I am Using AIX 5.3

Why not just set TZ to 4 hours before your current timezone:

eg (for New Delhi):

$ date
Fri Aug 12 10:27:01 IST 2011
 
$ TZ=GTM-1:30 date
Fri Aug 12 06:27:12 GTM 2011
1 Like

Unfortunately I don't have access to AIX. Does the below work on AIX? see if it works, if so we may be in luck. Normally this prints UNIX epoch time. If we get that from date +%s, we can play around with that.

date +%s or date '+%s', try both

---------- Post updated at 12:58 AM ---------- Previous update was at 12:57 AM ----------

I like that, very clever and easy, nice !

1 Like

Thanks for the Help,
Still i am looking for IF then Else part.

JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
if [ "$SysDate" == "$JobRunDate" ] && [ "$JobStTime" -ge "$CurSysHrs" ] && [ [ "x3" >= "x${JobStat}" ] || [ "x0" != "x${JobStat}" ] || [ "x2" != "x${JobStat}" ] ] 
then
{
echo "$JobName,$StartTime,$EndTime,$JobStat" >>$Jobstatus
}
fi

---------- Post updated at 10:19 AM ---------- Previous update was at 08:55 AM ----------

Thanks for the response.
But here i have to compare output numbers coming from two different time variables.
My question is i have to go 4 hours back from current time all the time in day(24hrs) and trying to capture the result list of jobs those executed in last four hours.

Pleae see entire code:

 
#!/usr/bin/sh
#Scripted by Raju B on Aug062011#
#Datastge Jobs monitoring script
cd /QIS2FTP/scripts/Scripts
#Removing Last Run Jobstatus.txt file.
rm /QIS2FTP/scripts/Scripts/Jobstatus.txt
#Printing Output in Jobstatus.txt
Jobstatus=/QIS2FTP/scripts/Scripts/Jobstatus.txt
echo "JobName,StartDateTime,EndDateTime,Status " >>$Jobstatus
echo "------------------------------------------------------------" >>$Jobstatus
#set -x
# import my environment
. /QIS2FTP/dsclientprod/home/my.env
#Variables declaration
#List of all jobs in DS QIS2 Project
DSJoblist=`dssearch -ljobs -matches -sub -oc QIS2 * |cut -f2 -d'\'|awk '{print $1}'`
Project=QIS2
SysDate=`date +%Y%m%d`
 
for i in $DSJoblist
do
#DSJobStatus variable for collecting Job start and End time,Job duration and Job status
DSJobStatus=`dsjob -report $Project $i 2> /dev/null | grep "Job" | cut -f2 -d'='`
StartTime=`echo $DSJobStatus | cut -f1,2 -d' '`
EndTime=`echo $DSJobStatus | cut -f3,4 -d' '`
JobStat=`dsjob -report $Project $i | grep status | awk -F= ' { print $2; } ' | awk ' { print $1; } '`;
JobName=`dssearch -ljobs -matches -sub -oc QIS2 $i`
#Dateformat 20110805
JobRunDate=`echo $StartTime |awk '{print $1}'|sed 's/-//'|sed 's/-//'`
#Time in hours ex: 09
JobTimeH=`echo $StartTime |awk '{print $2}'|cut -f1 -d':'`
JobTimeM=`echo $StartTime |awk '{print $2}'|cut -f2 -d':'`
JobStTime=`echo "$JobTimeH$JobTimeM"`
#Going 4 hour back from local time zone EST.
CurSysHrs=`TZ=GTM+1:00 date '+%H%M'`
#echo "Print Current time before 4 hours $CurSysHrs"
if [ "$SysDate" == "$JobRunDate" ] && [ "$CurSysHrs" -ge "$JobStTime" ]
then
if [ "x1" == "x${JobStat}" ]
then
echo "$JobName,$StartTime,$EndTime,$JobStat" >>$Jobstatus
fi
fi
# if [ "$SysDate" == "$JobRunDate" ] && [ "$CurSysHrs" -ge "$JobStTime" ]
# then if [ "x0" != "x${JobStat}" ]
# then 
# echo "$JobName,$StartTime,$EndTime,$JobStat" >>$Jobstatus
# 
# fi 
# fi
# if [ "$SysDate" == "$JobRunDate" ] && [ "$CurSysHrs" -ge "$JobStTime" ]
# then if [ "x2" != "x${JobStat}" ]
# then 
# echo "$JobName,$StartTime,$EndTime,$JobStat" >>$Jobstatus 
# fi 
# fi
done
#set +