[NTLUG:Discuss] How to tell file1 is half an hour older than file2
Paul Ingendorf
pauldy at wantek.net
Mon Apr 29 22:34:02 CDT 2002
I sat down and adjusted it to work with ksh and the version of ls I have your mileage may vary there otherwise this should illustrate better that it can be done sans the gnu tools.
I actually played with this on a few files and it seems to work fine in Linux.
However, in Solaris I don't think the ls supports the --full-time option and you have to mess with the LC_LOCAL to get it even close to the output. Of course you may just wish to ignore the year and do something special come years end.
The function you would want to mess with to tweak it for Solaris are
lsDate changing the values of the echo statement to get the appropriate month date time year output.
the ls --full-time would more than likely need to be changed to a simpler ls -l.
Notice the way the IFS is used to set a delimiter for the arg list much like the -F in my previous awk statements.
# ---------------------------------- Example -----------------------------------
# Author Paul Ingendorf
function returnMonthNum {
case "$1" in
Jan)
echo 1
;;
Feb)
echo 2
;;
Mar)
echo 3
;;
Apr)
echo 4
;;
May)
echo 5
;;
Jun)
echo 6
;;
Jul)
echo 7
;;
Aug)
echo 8
;;
Sep)
echo 9
;;
Oct)
echo 10
;;
Nov)
echo 11
;;
Dec)
echo 12
;;
*)
echo 0
esac
}
function returnSingle {
case "$1" in
00)
echo 0
;;
01)
echo 1
;;
02)
echo 2
;;
03)
echo 3
;;
04)
echo 4
;;
05)
echo 5
;;
06)
echo 6
;;
07)
echo 7
;;
08)
echo 8
;;
09)
echo 9
;;
*)
echo $1
esac
}
function returnHourMinSec {
# return single is used to avoid casting of 0X as octals in arithmetic operations
let hour=`returnSingle $1`
let minute=`returnSingle $2`
let second=`returnSingle $3`
let Time=$hour*60*60+$minute*60+$second
echo $Time
}
function returnTime {
IFS=:
let Time=`returnMonthNum $1`*24*60*60+$2*24*60*60+`returnHourMinSec $3`
unset IFS
echo $Time
}
function lsDate {
# this is adjusted until the date comes out as Month Date Time Year
echo $7 $8 $9 ${10}
}
function returnYear {
echo $4
}
fileDate1=`ls --full-time $1`
fileDate2=`ls --full-time $2`
fileTime1=`lsDate $fileDate1`
fileTime2=`lsDate $fileDate2`
if test `returnYear $fileTime1` -lt `returnYear $fileTime2`
then echo older than 30
else
fileSecs1=`returnTime $fileTime1`
fileSecs2=`returnTime $fileTime2`
let timeDiff=$fileSecs1-$fileSecs2
echo $timeDiff
# 1799 is 30 * 60 - 1 because it is easier
if test $timeDiff -gt 1799
then echo older than 30
else echo not older than 30
fi
fi
# -------------------------------- End Example ----------------------------------
-----Original Message-----
From: discuss-admin at ntlug.org [mailto:discuss-admin at ntlug.org]On Behalf
Of Sameer Khan
Sent: Sunday, April 28, 2002 10:21 PM
To: discuss
Subject: Re: [NTLUG:Discuss] How to tell file1 is half an hour older
than file2
Thanks all who responded (esp. Paul Ingendorf and Madhat who
wrote full blooded scripts!) The environment is Sun Solaris
2.6. Management doesn't want to put GNU stuff on their boxes
because "it's not supported" :,(
I'll cook something up based on Paul's logic.
Regards,
Sameer
_______________________________________________
http://www.ntlug.org/mailman/listinfo/discuss
More information about the Discuss
mailing list