我能够获取两个列表( /etc/passwd 和 /home ),但是如何编写脚本,例如读取 /etc/passwd 的行,解析主目录,然后在 /home 中查找。如果它不存在,抛出一个错误,如果它存在,继续前进。
用户的 /etc/passwd 主目录列表
cut -d":" -f6 /etc/passwd | grep home | sort
来自 /home 的用户列表
ls -1 /home | (while read line; do echo "/home/"$line; done)
也许直接从第一个命令输出到一个文件,然后将每一行读入一个 find 命令,然后......或者,用
if [ -d "$DIRECTORY" ]; then
echo "directory found for user that doesn't exist"
fi
现在如何将它们放在一起...
编辑:isedev 正是我所需要的。我的原始信息可能措辞有误……我们一直在清理用户,但没有清理他们的 /home 目录。所以我想知道仍然存在哪些没有 /etc/passwd 条目的 /home 目录。
这对 T 有用
for name in /home/*; do
if [ -d "$name" ]; then
cut -d':' -f6 /etc/passwd | egrep -q "^$name$"
if [ $? -ne 0 ]; then
echo "directory $name does not correspond to a valid user"
fi
fi
done
从现在开始,我们将奔跑
userdel -r login