Simpler Navigation for Servers and Operating Systems - Please Update Your Bookmarks
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
If you have bookmarked forums or discussion boards in Servers and Operating Systems, we suggest you check and update them as needed.
Showing results for 
Search instead for 
Did you mean: 

shell script help

Super Advisor

shell script help

i want to write a shell script


below is the output file



PS 030812 0 182 7


request is the password reset date 030812 is the 8th march 2012

182  days is password age


we need to write a script for all user, when ever user reach 170 days we send one auto reply mail to user, regarding your password expire.


Steve Post
Trusted Contributor

Re: shell script help

I have half an answer for you.   I put in a test at the bottom of my .profile for when I log in.  It is not perfect.  It is far from secure.  The program does not walk on water, or turn water into wine.   It uses basic perl. 


Best of all.  It works for me.  It warns me if my password is close to expiring.

The missing part of your request?  The email you want.


The main key to this is the /usr/bin/passwd  -r files -s unix_login_name_here


So if my unix login is Billy_bobby_billy_bo_bob, I run

/usr/bin/passwd -r file -s Billy_bobby_billy_bo_bob.


The 86400 is 60 seconds times 60 minutes times 24 hours.  I.e. number of seconds per day.



use Time::Local;
# todays date
( $T_sec, $T_min, $T_hour, $T_day, $T_mon, $T_year, @junk ) = localtime();
$T_epoch = timelocal( 0, $T_min, $T_hour, $T_day, $T_mon, $T_year);
($T_sec,$T_min,$T_hour,$T_day,$T_mon,$T_year,@junk ) = localtime($T_epoch);
$T_mon = $T_mon + 1;
$T_year = $T_year + 1900;
#print "Today is day $T_day month $T_mon yr $T_year\n";

# reset password date  R_xxxx
$CMD="\/usr\/bin\/passwd -r files -s $ENV{LOGNAME} |";
open(COMMAND, $CMD) || die "passwd cmd error";

while ( <COMMAND> ) {
  ( @line ) = split;
   if ( $#line == 4 ){
     if ( $line[3] < 10 and
          $line[4] > 50 and
          $line[4] < 80 and
          $line[1] eq "PS"  ) {
       # foreach $x (  @line ) { print "x is $x\n"; }
       # $line[0] name
       # $line[1] PS
       # $line[2] mm/dd/yy  $dline[0], $dline[1], $dline[2]
       # $line[3] < 10
       # $line[4] < 80 and > 50

       @dline = split(/\//, $line[2]);
       $R_epoch=timelocal( 0, 0, 0, $dline[1], $dline[0]-1, $dline[2]);
       $R_epoch=$R_epoch + ( $line[4] * 86400 );
       ($R_sec,$R_min,$R_hour,$R_day,$R_mon,$R_year ) = localtime($R_epoch);
       $R_year = $R_year + 1900;
       #print "Password day $R_day month $R_mon yr $R_year\n";

       # Dday is the reset required date - warning days
       $D_epoch=$R_epoch - ( $WARN * 86400 );
       ($D_sec,$D_min,$D_hour,$D_day,$D_mon,$D_year ) = localtime($D_epoch);
       $D_year = $D_year + 1900;
       #print "D-day        $D_day month $D_mon yr $D_year\n";

       if ( $T_epoch >= $D_epoch ) {
           print "User $ENV{LOGNAME}, You need to change our password soon.\n";
           printf("Today is day %02d/%02d/%04d\n", $T_mon, $T_day, $T_year);
           printf("Warn day is  %02d/%02d/%04d\n", $D_mon, $D_day, $D_year);
           printf("Password day %02d/%02d/%04d\n", $R_mon, $R_day, $R_year);
       } else {
           print "User $ENV{LOGNAME}, ";
           printf("the day your password dies is %02d/%02d/%04d\n",
                $R_mon, $R_day, $R_year);