- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: compare 2 dates ..
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:28 AM
07-16-2001 11:28 AM
how would i go about comparing 2 dates for example:
DATE1=7-16-2001
DATE2=7-12-2001
if [$DATE1 > $DATE2]
then
another command
else
this command
fi
I can get it to work with numbers but not dates.
Thanks
Richard
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:35 AM
07-16-2001 11:35 AM
Re: compare 2 dates ..
In the example, with the format you cited, do:
if [ $DATE1 -gt $DATE2 ]
...but it's better to test date relationships by constructing the date as CCYYMMDD. So, in this case, I'd use 'cut' or 'awk' to rearrange the string before testing it.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:38 AM
07-16-2001 11:38 AM
SolutionI have the perfect answer for this. The way to do this is convert a MM DD YYYY into a Julian Day (~ days since 01/01/4712 BCE - used by astronomers to make calculations easy).
Use it like this:
DAY1=`caljd.sh 07/16/2001`
DAY2=`caljd.sh 07/19/2001`
You can then do numerical operations on the two
values. The routine when supplied with 1 arg will assume that it is a Julian Day and convert it to a calander day. It's also very handy for taking a date and find out what the date is 37 days from then. You can also use it to determine the day of the week. caljd.sh -u will give full usage.
Enjoy, Clay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:39 AM
07-16-2001 11:39 AM
Re: compare 2 dates ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:42 AM
07-16-2001 11:42 AM
Re: compare 2 dates ..
This is a example for you.
This is script find date a given number of days ago :
===========================================
set printnums = 0
# Check for -n switch. Print day & month in numbers rather than word (i.e. 0-6 and 1-12)
if ("$1" == "-n") then
set printnums = 1
shift
endif
if ("$2" == "-n") set printnums = 1
@ offset = `echo $1 | awk '{printf("%d",$0)}'`
if ($offset < 1) then
echo "Usage: `basename $0` [-n]
exit 1
endif
if ($offset > 730000) then
echo "Get real. This is before the birth of Christ."
exit 1
endif
@ year = `date +'%Y'`
@ newyear = $year
set dayname = `date +'%a'`
set newdayname = $dayname
@ monthnum = `date +'%m'`
@ newmonthnum = $monthnum
set month = `date +'%b'`
set newmonth = $month
@ datenum = `date +'%e'`
@ newdatenum = $datenum
@ daynum = `date +'%u'`
@ newdaynum = $daynum
if (($datenum - $offset) > 0) then # If same month
@ newdatenum = ($datenum - $offset)
else # Otherwise ...
@ newdatenum = ($datenum - $offset) # newdatenum is now negative
while ($newdatenum < 1)
@ monthnum--
if ($monthnum < 1) then
@ monthnum = 12
@ newyear--
endif
set newmonth = `echo $monthnum | awk 'BEGIN{month[1]="Jan";month[2]="Feb";month[3]="Mar";month[4]="Apr";month[5]="May"; month[6]="Jun";month[7]="Jul";month[8]= "Aug";month[9]="Sep";month[10]="Oct"; month[11]="Nov";month[12]="Dec"}{print month[$0]}'`
switch ($newmonth)
case Apr:
case Jun:
case Sep:
case Nov:
@ newdatenum += 30
breaksw
case Feb:
if (($newyear % 4) == 0) then
@ newdatenum += 29 # Leap year
else
@ newdatenum += 28 # Non leap year
endif
breaksw
default:
@ newdatenum += 31
endsw
end
endif
# Calc dayname
@ tmp = ($offset % 7)
if (($daynum - $tmp) >= 0) then
@ daynum -= $tmp
else
@ daynum += (7 - $tmp)
endif
set newdayname = `echo $daynum | awk 'BEGIN{day[0]="Sun";day[1]="Mon";day[2]="Tue";day[3]="Wed";day[4]="Thu"; day[5]="Fri";day[6]="Sat";day[7]="Sun"}{print day[$0]}'`
# Print results
if ($printnums == 1) then
printf "%.1d %.2d %.2d %.4d\n" $daynum $monthnum $newdatenum $newyear
else
printf "%3s %3s %2d %4d\n" $newdayname $newmonth $newdatenum $newyear
endif
exit 0
==============================================
I think this help.
Regards,
Abel Berger
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 11:47 AM
07-16-2001 11:47 AM
Re: compare 2 dates ..
#!/usr/local/bin/perl
chomp @listsystems = ( list of systems);
%User = ();
open(FILE,">who.list")|| die "who.list cannot be opened\n";
chomp($Curr_Month = `/usr/bin/date +%b`);
chomp($Curr_Date = `/usr/bin/date +%d`);
chomp($Curr_Weekday = `/usr/bin/date +%u`);
print FILE "#$system\n";
@whoinfo = `remsh $system -n who -u |grep old |sort`;
foreach (@whoinfo){
if (/console/) {
next;
}
($uname,$t2,$Month,$Date,$t3,$t4,$pid,$t6) = split(/\s+/,$_);
$result=`remsh $system -n ps -ef |grep $pid|grep $uname`;
# print "$uname $pid $system\n";
# print "$result\n";
# $wait=
if ($result eq ""){ #checking for valid login
next;
}
if ($Month ne $Curr_Month){
if ($Curr_Date > 10){
$User{$uname}++;
print FILE "$_";
next;
}
if ($Curr_Date < 10 && $Date < 25){
$User{$uname}++;
print FILE "$_";
next;
}
}
if ($Curr_Date > 15 && $Date < 5){
#print "2";
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}
if ($Curr_Date > 20 && $Date < 10){
chomp;
$User{$uname}++;
print FILE "$_ more then 10 Days old\n";
next;
}
}#whoinfo
}#listsystems
close(FILE);
foreach $user (sort keys %User){
open(FILE,"who.list")||die "Can't open who.list file\n";
open(TEMP,">user.old")|| die "Can't open user.old file\n";
$sum=0;
print TEMP "Following are your old logins check them and kill them if necessary\n";
print TEMP "using listed PID \n";
print TEMP "If you are not able to logout or kill them contact support\n\n";
print TEMP "NAME LINE TIME IDLE PID COMMENTS\n";
$found=0;
while(
($uname,$t2) = split(/\s+/,$_);
if (/^#/) {
$sysname=$_;
$sysname_print=1; #print sys name if it is new system
}
if ($user eq $uname){
if ($sysname_print == 1){ # print system name and
print TEMP "\n$sysname";
$sysname_print=0; # make variable =0 so next time it will not print sysname
}
$found=1;
print TEMP "$_";
$sum++;
}
} #while
print TEMP "\n\nThanks for your help\n\n\n";
close(TEMP);
close(FILE);
# $user1="sachin";
if ($found == 1){ # send mail only if found any old login
# print "send to $user";
system("/usr/bin/cat /home/sachin/myscript/cronjobs/user.old");
system("/usr/bin/cat /home/sachin/myscript/cronjobs/user.old | /bin/mailx -s 'OLD LOGIN' $user");
}
#$wait=
}#foreach
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 12:04 PM
07-16-2001 12:04 PM
Re: compare 2 dates ..
Oops! I missed that fact that you were comparing stings, not integers. You could strip the "-" out and compare as two numbers or extract and rearrange before comparing:
# X=`echo $DATE1|tr -d "-"`
# Y=`echo $DATE2|tr -d "-"`
...or:
# X=`echo $DATE1|awk -F- '{print $3 $1 $2}'`
# Y=`echo $DATE2|awk -F- '{print $3 $1 $2}'`
...and then test as integers:
if [ $X -gr $Y ]
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-16-2001 12:28 PM
07-16-2001 12:28 PM
Re: compare 2 dates ..
I didn't notice a small mistake in my posting that I'm sure you've caught; it should be:
DAY1=`caljd.sh 07 16 2001` rather that 07/16/2001
It needs the three args to know this a calendar date and I want a Julian Day rather than the reverse.
Clay