#!/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这段代码去掉筛选非系统用户
时间: 2023-12-10 11:37:46 浏览: 22
可以将第4行改为:
```
users=$(cut -d: -f1 /etc/passwd | grep -v -E "nologin|false|sync|shutdown|halt")
```
这样就会排除掉系统用户了。
相关问题
#!/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
```
用shell写一个脚本,编写程序,将/etc/ passwd的第一栏取出,而且每一栏都以一行字符串“The1 account is "root””显示,其中1表示行数。
可以使用如下的shell脚本实现:
```shell
#!/bin/bash
# 读取/etc/passwd文件的第一栏
users=$(cut -d: -f1 /etc/passwd)
# 遍历每个用户名,并输出对应的信息
count=1
for user in $users
do
echo "The$count account is $user"
count=$((count+1))
done
```
这个脚本首先使用`cut`命令获取/etc/passwd文件的第一栏(即用户名),然后使用循环遍历每个用户名,并输出对应的信息。其中,`$count`表示行数,`$user`表示当前用户名。注意要使用`$((count+1))`进行变量的自增操作。