Operating System - HP-UX
1831645 Members
1913 Online
110029 Solutions
New Discussion

Shell scripts for calculating the number of days betn DATES

 
SOLVED
Go to solution
ShivKumar_1
Frequent Advisor

Shell scripts for calculating the number of days betn DATES

Hi Experts
I am trying to write a script which calculates the difference between the two dates and gives it as the number of days say for Eg: between today 4/18/01 and 3/18/01 its 30 days.
My aim is to monitor the last login time of a user and compare it with the system date and in case its more than 60 days I want to send a alert saying user has been idle. My client runs a NIS environment and he doesnt havee trusted systems. I have tried writing the following script below, can you pl guide me and let me know if its the correct way?

Thnks in advance
Shiv
_______________________________________________

#! /bin/sh
OL=`TZ=PST+1440 date +%m%d`
TO=`date +%m%d`
LST=`last -1 karin | awk '{print $6}'`
MON=`last -1 karin | awk '{print $5}'`
case $MON in
Jan) MO=01;;
Feb) MO=02;;
Mar) MO=03;;
Apr) MO=04;;
May) MO=05;;
Jun) MO=06;;
Jul) MO=07;;
esac
CK=$MO$LST
echo Last Login Date $CK
if [ $OL -lt $MO ]
then
echo This user has not loged last 60 days;
echo This user has to be deleted ;
else
echo User logged within 60 days;
fi
8 REPLIES 8
Thierry Poels_1
Honored Contributor
Solution

Re: Shell scripts for calculating the number of days betn DATES

Hi,
instead of really calculating the difference between the two dates, and relying on the fact that 'last' can supply the last login date,
I would go for another option:
- touch a file in their home dir each time they login (touch $HOME/.lastlogin in their .profile)
- you can check if this file is recent with find:
--- find $HOME -name '.lastlogin' -mtime +60

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
James R. Ferguson
Acclaimed Contributor

Re: Shell scripts for calculating the number of days betn DATES

Hi:

I think Thierry's approach is the most direct. I would only add two comments:

1. 'last' is an excellent choice. It will only work if you have maintain (first touch) '/var/adm/wtmp'. See the man pages for 'last'. Depending on the number of users and logins you might not want to keep upwards of 60-days of information this way.

2. Assuming a posix or ksh shell is being used, you could track the HISTFILE file (normally $HOME/.sh_history) as your reference point.

...JRF...
Rita C Workman
Honored Contributor

Re: Shell scripts for calculating the number of days betn DATES

I also found this thread that included a script for date calculating that you may want to check out also:

Look for Al Langen post:
http://forums.itrc.hp.com/cm/QuestionAnswer/1,1150,0x57eff841489fd4118fef0090279cd0f9,00.html

/rcw
Byron Myers
Trusted Contributor

Re: Shell scripts for calculating the number of days betn DATES

This is how I do such things. I wrote a small C program that gets the number of seconds since an epoch. Then I log this number somewhere. Later, I run the program again and log this number. Subtract the first number from the second and you get the number of seconds since you ran the command. You can divide the difference by 60 to get minutes, and by 3600 to get hours since the commands were run. This way you don't have to mess with leap years, crossing month and year boundaries, daylight savings changes, etc.. Here is the C code:
#include
main(argc,argv)
int argc;
char ** argv;
{
time_t num_secs;
num_secs=time(NULL);
printf("%u",num_seecs);
}

Hope this helps.
If you can focus your eyes far and straight enough ahead of yourself, you can see the back of your head.
A. Clay Stephenson
Acclaimed Contributor

Re: Shell scripts for calculating the number of days betn DATES

I like Thierry's approach for your problem but if you really want a nifty date calculator I've attached one that generate a small awk script and calculates a julian date (used by astronomers to essentially calculate an absolute number of days since ~4004 BCE).
You simply call the shell function
julian_day MM/DD/YYYY and it returns a number.
Call it with another date and subtract the two and ther you are.
If it ain't broke, I can fix that.
Christopher Caldwell
Honored Contributor

Re: Shell scripts for calculating the number of days betn DATES

If you're running Byron's C program, and you're running in Trusted mode, there's a field in the user's tcb record that stores the users last successful login.

Retrieve the field by doing something like

mypr_passwdent=getprpwent();

days_between=(time(NULL)-(&mypr_passwdent->ufld.fd_slogin))/86400

printf("%d days since last login",days_between);

Note: this is code is meant as an example, man is your friend; this code is incomplete, but should give a C programmer enough to succesfully write a working utility.
Vincenzo Restuccia
Honored Contributor

Re: Shell scripts for calculating the number of days betn DATES

See the attachment.
ShivKumar_1
Frequent Advisor

Re: Shell scripts for calculating the number of days betn DATES

Hi Experts
Thnks for the help,