2

我能够获取两个列表( /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

4

3 回答 3

2

This will report all home directories from /etc/passwd that should be in /home but aren't:

cut -d":" -f6 /etc/passwd | grep home | sort | 
    while read dir; do [ -e "$dir" ] || echo Missing $dir; done

And this one reports all that don't exist:

cut -d":" -f6 /etc/passwd | while read dir; do 
    [ -e "$dir" ] || echo Missing $dir
done
于 2014-10-07T19:53:23.823 回答
2

作为第一个近似值:

perl -F: -lane 'next if m/^#/;print "$F[5] for user $F[0] missing\n" unless(-d $F[5])' /etc/passwd

/etc/passwd如果你想找出 the和 the之间的区别/home

comm <(find /home -type d -maxdepth 1 -mindepth 1 -print|sort) <(grep -v '^#' /etc/passwd  | cut -d: -f6| grep '/home' | sort)

狭窄的形式

comm    <(
            find /home -type d -maxdepth 1 -mindepth 1 -print |sort
        ) <(
            grep -v '^#' /etc/passwd  |cut -d: -f6 |grep /home |sort
        )

如果你会使用

  • comm ...(如上没有参数)将显示 3 列 1.) 仅在 /home 2.) 仅在 /etc/passwd 3.) 常见
  • comm -23 ....- 将显示仅在 /home 中的目录(而不是在/etc/passwd
  • comm -13 ....- 将向目录显示仅在 /etc/passwd 中而不在/home
  • comm -12 ....- 将显示正确的目录(存在于 the/etc/passwd和 the 中/home

我不确定-{max|min}depthAIX 上的 ..

于 2014-10-07T19:44:34.173 回答
1

因此,假设您想知道 /home 下是否有与现有用户不对应的目录:

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

再一次,这假设您没有使用诸如 LDAP 或 NIS 之类的名称服务,在这种情况下,将以下列开头的行更改cut为:

getent passwd | cut -d':' -f6 | egrep -q "^$name$"
于 2014-10-07T19:51:21.880 回答