#!/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" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "never" || $(chage -l $user | grep "Password expires" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "从不" ]] 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帮我把这段代码的第一个if去掉
时间: 2023-12-10 22:37:55 浏览: 87
好的,这是去掉第一个if之后的代码:
```bash
#!/bin/bash
users=$(cut -d: -f1 /etc/passwd)
for user in $users
do
if [[ $(chage -l $user | grep "Password expires" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "never" || $(chage -l $user | grep "Password expires" | grep "密码过期时间" | cut -d: -f2 | tr -d ' ') == "从不" ]]
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
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
```
注意,去掉了第一个if语句后,需要将原代码中的第二个if移动到第一个if的位置,以保证逻辑正确。
阅读全文