【发布时间】:2015-09-21 11:01:05
【问题描述】:
我需要在密码到期天数时通知 unix box 用户,因为我使用了以下脚本。
#!/bin/sh
rcvr1=test1@testvm.localdomain.com
rcvr2=test2@testvm.localdomain.com
for i in babinlonston lonston babin
do
# convert current date to seconds
currentdate=`date +%s`
# find expiration date of user
userexp=`chage -l $i |grep 'Password expires' | cut -d: -f2`
# convert expiration date to seconds
passexp=`date -d “$userexp” +%s`
# find the remaining days for expiry
exp=`expr \( $passexp – $currentdate \)`
# convert remaining days from sec to days
expday=`expr \( $exp / 86400 \)`
if [ $expday -le 10 ]; then
echo “Please do the necessary action” | mailx -s “Password for $i will expire in $expday day/s” $rcvr3,$rcvr2
fi
done
当我运行脚本时,我得到以下错误。
[root@testvm ~]# sh script.sh
date: extra operand `23,'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
date: extra operand `+%s'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
date: extra operand `+%s'
Try `date --help' for more information.
expr: syntax error
expr: syntax error
script.sh: line 20: [: -le: unary operator expected
[root@testvm ~]#
我该如何解决这个问题。而不是 -le 我需要使用什么选项。
【问题讨论】:
-
您是否需要将其作为 bourne shell 脚本运行,或者是否可以使用 bash 的附加功能?
-
您使用了错误的引号类型:
date -d “$userexp” +%s应该是date -d "$userexp" +%s。其他印刷引号也必须替换为常规 ASCII 引号。