【问题标题】:Shell script to send email before password expires在密码过期之前发送电子邮件的 Shell 脚本
【发布时间】: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 引号。

标签: bash shell sh


【解决方案1】:

不要将它作为 sh ./script 运行 - 这将在 sh shell 中运行它。 作为 ./script 运行它

我对其进行了一些修改,使其更加“现代”。

#!/bin/bash
#

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 | awk '/^Password expires/ { print $NF }')

  if [[ ! -z $userexp ]]
  then

    # convert expiration date to seconds
    passexp=$(date -d "$userexp" "+%s")

    if [[ $passexp != "never" ]]
    then
      # find the remaining days for expiry
      (( exp = passexp - currentdate))

      # convert remaining days from sec to days
      (( expday =  exp / 86400 ))

      if ((expday < 10 ))
      then
        echo "Please do the necessary action"  | mailx -s "Password for $i will expire in $expday day/s" $rcvr3,$rcvr2
      fi
    fi
  fi

done

【讨论】:

    猜你喜欢
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-17
    • 2014-06-17
    • 2010-12-19
    • 1970-01-01
    相关资源
    最近更新 更多