Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if File Greater Than n Hours

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In a Bourne shell script on a Solaris 8 system I am creating a flag file (/tmp/myscript.flag) when a certain action is performed. Though this script runs every 15 minutes I do not want to perform this action until my flag file is older than n hours (n may be: 6, 12 or 24 hours).

How can I test to see if the date/time of my currently running script greater than n hours from my flag file date/time?

Thanks,

Michael42
 
PHV,

>> Can't you use a Korn shell ?

I am sorry to say that is not an option for me.

Thanks,

Michael42
 
I'm too lazzy to convert this to Bourne syntax but you may use it as a starting point:
Code:
GetDate(){ # GetDate nHours [format]
  typeset -i nHours=$1; format=$2
  eval $(echo $TZ | sed '
s!\([^-0-9]*\)\([-0-9]*\)\(.*\)!typeset -i localOffset=\2;zon1=\1;zon2=\3!')
  TZ=$zon1$((localOffset-nHours))$zon2 date $format
}
n=6 'or 12 or 24
touch -t $(GetDate -$n +%Y%m%d%H%M) /tmp/myscript.tst
[ -n "$(find /tmp/myscript.flag -newer /tmp/myscript.tst)" ] && exit

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Stefan, I'm not sure the Solaris 8 find command supports all the non standard extensions of the linux/GNU versions ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
If you have perl installed...
Code:
SECS=`perl -e '@q=stat($ARGV[0]); print time-$q[9]' flagfile`
if [ $SECS -lt 10800 ]
then
    echo Less than 3 hours since last run
    exit
fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top