Operating System - HP-UX
1819803 Members
3205 Online
109607 Solutions
New Discussion юеВ

date calculation/conversion help needed

 
Kristopher March
Regular Advisor

date calculation/conversion help needed

I'm using the passwd -sa command to determine when a users password will/should expire.

The third field shows the date of the last modification date of their password. If I add 89 days to it, I'll have the exact date the password will expire. I intend to send a e-mail to a user 14 days prior to the account expiring. I also plan to have an e-mail sent every day after the 14 day mark reminding the user if the password hasn't been changed.

Can someone suggest a way for me to calculate the date using the format used by running passwd -sa?

Here's an example output:

emxadm PS 04/27/04 0 90 14
"This ain't no burger flippin job!"
10 REPLIES 10
Steven E. Protter
Exalted Contributor

Re: date calculation/conversion help needed

http://www.hpux.ws/merijn/caljd.sh
http://www.hpux.ws/merijn/caljd.pl

Two great utilities that I use for date calculations.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Kristopher March
Regular Advisor

Re: date calculation/conversion help needed

yes, thanks. I'm reading through some old posts on this utility at this very moment.

I'll get back to you with questions.
"This ain't no burger flippin job!"
A. Clay Stephenson
Acclaimed Contributor

Re: date calculation/conversion help needed

Okay, this should get you started; we need to capture the 3rd field of the passwd -sa command:

passwd -sa | awk '{if $3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}' | while read DT
do
DT89=$(caljd.sh -C -S "/" $(caljd.sh -c -S "/" -C -n 89 ${DT}))
echo "${DT} --> ${DT89}"
done

If I haven't made any of them there typo's that there should do it. You will need a fairly new version of caljd.sh to convert 2 digit years into 4 digit recent years (-C argument).

Here is a version of caljd.sh that will suffice. Invoke as caljd.sh -u for full usage and examples.

If it ain't broke, I can fix that.
Kristopher March
Regular Advisor

Re: date calculation/conversion help needed

Thanks Clay. I renamed the script to sa temporarily. I'm running it and getting the following:

# ./sa
+ passwd -sa
+ read DT
+ awk {if $3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}
syntax error The source line is 1.
The error context is
{if >>> $3 <<< ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}
awk: The statement cannot be correctly parsed.
The source line is 1.
awk: There is an extra ) character.


I don't know enough about what's going on in it to debug. Please help.
"This ain't no burger flippin job!"
A. Clay Stephenson
Acclaimed Contributor

Re: date calculation/conversion help needed

Well, my advanced hunt-and-peck typing technique has done gone and got me in trouble again. I missed and opening parenthesis in the awk if statement:

'{if $3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}'

should be:

'{if ($3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}'

Let him out with that there syntax and let's see if that dog will hunt now.
If it ain't broke, I can fix that.
Kristopher March
Regular Advisor

Re: date calculation/conversion help needed

Awesome! It works great.

# ./sa
12/30/03 --> 03/28/04
05/11/04 --> 08/08/04
12/01/03 --> 02/28/04
12/16/03 --> 03/14/04
08/02/02 --> 10/30/02
02/11/04 --> 05/10/04
00/00/00 --> 02/27/00
00/00/00 --> 02/27/00
00/00/00 --> 02/27/00
12/16/03 --> 03/14/04
01/13/04 --> 04/11/04
00/00/00 --> 02/27/00
00/00/00 --> 02/27/00
05/20/04 --> 08/17/04
04/03/03 --> 07/01/03
04/27/04 --> 07/25/04
03/16/04 --> 06/13/04

I really appreciate your time on this. I only have one other request, if I may be permitted to do so. Can you help me get this to work in Korn? I'd like to use it on other platforms.
When I get the final script completed I'll post it here and share it with you all.
"This ain't no burger flippin job!"
A. Clay Stephenson
Acclaimed Contributor

Re: date calculation/conversion help needed

Well, it's extremely complicated. You need to change the "shebang" #!/usr/bin/sh in caljd.sh to #!/usr/bin/ksh. That's it and because I feel a Sunrise sneaking in here, caljd.sh will automatically use nawk (new awk) instead of awk. I don't know why Sun hasn't renamed (or linked) nawk to awk because nawk has been around for about 15 years now.

The code that invokes awk in the sample script does nothing special so awk, oawk, nawk, or gawk should work.
If it ain't broke, I can fix that.
Kristopher March
Regular Advisor

Re: date calculation/conversion help needed

Obviously I'm ignorant when it comes to this;

I changed caljd.sh to read #!/usr/bin/ksh
and the sa script to the same:

# vi sa
"sa" 7 lines, 206 characters
#!/usr/bin/ksh
set -x
passwd -sa | awk '{if ($3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}' | while read DT
do
DT89=$(caljd.sh -C -S "/" $(caljd.sh -c -S "/" -C -n 89 ${DT}))
echo "${DT} --> ${DT89}"
done


I get this now:

# ./sa
+ passwd -sa
+ read DT
+ awk {if ($3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}
awk: syntax error near line 1
awk: illegal statement near line 1
"This ain't no burger flippin job!"
A. Clay Stephenson
Acclaimed Contributor

Re: date calculation/conversion help needed

Well, there's something you're not telling us like : what platform/OS are you trying this on. The syntax should have worked in the ksh. I suspect the problem is awk. If this is Solaris then change the reference to awk to nawk OR try this (I'm guessing)

the regular expression is looking for exactly 2 digits

passwd -sa | awk '{if ($3 ~ "[0-9]{2}/[0-9]{2}/[0-9]{2}") print $3}'

change it to:

passwd -sa | awk '{if ($3 ~ "[0-9][0-9]/[0-9][0-9]/[0-9][0-9]") print $3}'

AND if that doesn't work change awk to nawk.
If it ain't broke, I can fix that.
Kristopher March
Regular Advisor

Re: date calculation/conversion help needed

Thanks again. I got it to work on HP-UX 11.

I also changed awk to nawk in your last suggestion and it now works in Sol5.8

"This ain't no burger flippin job!"