bash - Shell script to send email before password expires -
i need notify unix box users when there password going expires in number of days have used below script.
#!/bin/sh rcvr1=test1@testvm.localdomain.com rcvr2=test2@testvm.localdomain.com in babinlonston lonston babin # convert current date seconds currentdate=`date +%s` # find expiration date of user userexp=`chage -l $i |grep 'password expires' | cut -d: -f2` # convert expiration date seconds passexp=`date -d “$userexp” +%s` # find remaining days expiry exp=`expr \( $passexp – $currentdate \)` # convert remaining days sec days expday=`expr \( $exp / 86400 \)` if [ $expday -le 10 ]; echo “please necessary action” | mailx -s “password $i expire in $expday day/s” $rcvr3,$rcvr2 fi done
when ever run script below error.
[root@testvm ~]# sh script.sh date: operand `23,' try `date --help' more information. expr: syntax error expr: syntax error script.sh: line 20: [: -le: unary operator expected date: operand `+%s' try `date --help' more information. expr: syntax error expr: syntax error script.sh: line 20: [: -le: unary operator expected date: operand `+%s' try `date --help' more information. expr: syntax error expr: syntax error script.sh: line 20: [: -le: unary operator expected [root@testvm ~]#
how can slove issue. instead of -le option need use.
don't run sh ./script - run in sh shell. run ./script
i've amended , made more "modern".
#!/bin/bash # rcvr1=test1@testvm.localdomain.com rcvr2=test2@testvm.localdomain.com in babinlonston lonston babin # convert current date seconds currentdate=$(date +%s) # find expiration date of user userexp=$(chage -l $i | awk '/^password expires/ { print $nf }') if [[ ! -z $userexp ]] # convert expiration date seconds passexp=$(date -d "$userexp" "+%s") if [[ $passexp != "never" ]] # find remaining days expiry (( exp = passexp - currentdate)) # convert remaining days sec days (( expday = exp / 86400 )) if ((expday < 10 )) echo "please necessary action" | mailx -s "password $i expire in $expday day/s" $rcvr3,$rcvr2 fi fi fi done
Comments
Post a Comment