locked=$(passwd -S "$user" | awk '{print $2}')结果是ps
时间: 2024-02-28 12:52:53 浏览: 46
如果 `locked=$(passwd -S "$user" | awk '{print $2}')` 的结果是 `ps`,那么这可能是因为 `$user` 变量没有被正确设置,导致 `passwd -S` 命令无法正确地获取用户的密码状态。
`passwd -S` 命令的输出格式为:
```
username status chage min max warn inactive expire flag
```
其中,`status` 字段表示密码状态,可能是以下值之一:
- `P`:密码已锁定;
- `L`:密码已过期;
- `NP`:密码尚未设置;
- `NL`:密码尚未设置且已过期;
- 空字符串:密码未被锁定或过期。
因此,`awk '{print $2}'` 命令将提取 `status` 字段的值,即用户的密码锁定状态。
请检查 `$user` 变量是否正确设置,并确保该用户存在于系统中。
相关问题
#!/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语法问题
在第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
```
#!/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这段代码去掉筛选非系统用户
可以将第4行改为:
```
users=$(cut -d: -f1 /etc/passwd | grep -v -E "nologin|false|sync|shutdown|halt")
```
这样就会排除掉系统用户了。
阅读全文