[clug] incrementing time
Stephen Jenkin
sjenkin at pcug.org.au
Thu Nov 6 00:18:55 EST 2003
Boris et al,
Here's a little bash script that will check a number of files and call a
command on files older than xxx seconds...
Uses: '-ot' in test.
[ file1 -ot file2 ] ==> true if file1 older than file2
and the GNU touch, '-B secs' ==> backwards some seconds.
For portability, It is possible to use something like:
touch `TZ=xxx-10yyy date +%M%D%H%M`
but fooling around with the TZ variable and coping with daylight saving is
really painful. Look at zdump output for Australia/B=NSW: There is EST &
EST!! Standard time and Summer time. To tell in a script [date +%Z] if
it's summer time or not, requires building a new timezone [found zic but
not the input file] with a different name for summer time.
In the US EST/EDT is used.
Here is example output of date with TZ changed:
[@cdr root]# date
Thu Nov 6 00:10:49 EST 2003
[@cdr root]# date +%m%d%H%M
11060010
[@cdr root]# TZ=xxx-10 date +%m%d%H%M ((in not-summer, use -9))
11052310
OK, time for the script. A few options as leads...
Hope this long expo was useful :-)
cheers
sj
======================================================================
#!/bin/bash
# /stevej/bin/chk_file
# Steve Jenkin Wed Nov 5 23:15:52 EST 2003
PRG=`basename $0`
tmpfile=/tmp/$PRG.$$
trap "rm $tmpfile" EXIT
back=3600
cmd=
invert=0
verbose=0
while getopts ':B:c:xisv' opt
do
case $opt in
B ) back=$OPTARG ;;
c ) cmd="$OPTARG" ;;
x ) set -x ;;
i ) invert=1 ;;
s ) verbose=0 ;;
v ) verbose=1 ;;
* ) echo "$OPTARG Not recognised" >&2; exit 1 ;;
esac
done
shift $(( $OPTIND - 1 ))
OPTIND=0
touch -B $back $tmpfile
for file in "$@"
do
if [[ "$file" -ot $tmpfile ]]
then
if [[ $invert -eq 0 ]] && [[ -n "$cmd" ]]
then
$cmd "$file"
else
[[ $verbose -eq 1 ]] && echo "${file}: older"
fi
else
if [[ $invert -ne 0 ]] && [[ -n "$cmd" ]]
then
$cmd "$file"
else
[[ $verbose -eq 1 ]] && echo "${file}: newer or same"
fi
fi
done
======================================================================
Steve Jenkin, Unix Sys Admin
0412 786 915 (+61 412 786 915)
PO Box 48, Kippax ACT 2615, AUSTRALIA
On Wed, 5 Nov 2003, Rousak, Boris wrote:
>
> Greeting all,
>
> This is a bit basic but I am having a mind blank :). I am trying to write a
> script that will check a time stamp written in a file to see if it has been
> an hour since it was made. The stamp is in a format, Nov 03 07:32:06. I am
> using Ksh and the only way possible that I could find (google and all) is to
> convert the date to time since 1970 and do a number comparison. This is
> however a hassle, so the question is: can anyone suggest an easier way to do
> this, awk maybe or something to that effect. I have looked at perl, but that
> requires an extra module download, namely: Date::Manip and this is not an
> option for me. If anyone can hit me with a clue-bat that would be much
> appreciated.
>
> Regards,
> Boris
>
>
>
> ************************************************************************
> *PLEASE NOTE* This email and any attachments may
> be confidential. If received in error, please delete all
> copies and advise the sender. The reproduction or
> dissemination of this email or its attachments is
> prohibited without the consent of the sender.
>
> WARNING RE VIRUSES: Our computer systems sweep
> outgoing email to guard against viruses, but no warranty
> is given that this email or its attachments are virus free.
> Before opening or using attachments, please check for
> viruses. Our liability is limited to the re-supply of any
> affected attachments.
>
> Any views expressed in this message are those of the
> individual sender, except where the sender expressly,
> and with authority, states them to be the views of the
> organisation.
> ************************************************************************
>
More information about the linux
mailing list