#!/bin/bash users=$(cut -d: -f1 /etc/passwd) for user in $users do if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 ]] then if [[ $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ') == "never" ]] then echo "$user: Password never expires" else expire_date=$(date -d "$(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" +%s) current_date=$(date +%s) if [[ $expire_date -lt $current_date ]] then echo "$(hostname -I | awk '{print $1}'),$user: Password has expired" else echo "$user: Password will expire on $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" fi fi else echo "" account_status=$(passwd -S $user | awk '{print $2}') if [[ "$account_status" == "L" ]]; then echo "$(hostname -I | awk '{print $1}'),User $user: account locked!" fi done语法问题
时间: 2023-11-22 20:55:37 浏览: 113
Shell脚本bash: ./t.sh:/bin/bash^M:损坏的解释器: 没有那个文件或目录
在第27行缺少一个 `fi` 关键字来结束第20行的 `if` 语句。同时,第24行的 `echo` 命令中,应该把逗号放在双引号内,否则会被视为两个参数。正确的脚本如下:
```
#!/bin/bash
users=$(cut -d: -f1 /etc/passwd)
for user in $users
do
if [[ $(getent passwd $user | cut -d: -f3) -ge 1000 ]]
then
if [[ $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ') == "never" ]]
then
echo "$user: Password never expires"
else
expire_date=$(date -d "$(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')" +%s)
current_date=$(date +%s)
if [[ $expire_date -lt $current_date ]]
then
echo "$(hostname -I | awk '{print $1}'), $user: Password has expired"
else
echo "$user: Password will expire on $(chage -l $user | grep "Password expires" | cut -d: -f2 | tr -d ' ')"
fi
fi
else
account_status=$(passwd -S $user | awk '{print $2}')
if [[ "$account_status" == "L" ]]; then
echo "$(hostname -I | awk '{print $1}'), User $user: account locked!"
fi
fi
done
```
阅读全文