Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 在/etc/passwd中没有条目的unix主目录_Shell_Unix_Directory_Passwd - Fatal编程技术网

Shell 在/etc/passwd中没有条目的unix主目录

Shell 在/etc/passwd中没有条目的unix主目录,shell,unix,directory,passwd,Shell,Unix,Directory,Passwd,我能够获得这两个清单(/etc/passwd和/home),但是如何编写脚本,比如读取/etc/passwd的行,解析home目录,然后在/home中查找它。如果它不存在,抛出一个错误,如果它确实存在,继续前进 /用户的etc/passwd home dir列表 cut -d":" -f6 /etc/passwd | grep home | sort 来自/主页的用户列表 ls -1 /home | (while read line; do echo "/home/"$line; done)

我能够获得这两个清单(/etc/passwd和/home),但是如何编写脚本,比如读取/etc/passwd的行,解析home目录,然后在/home中查找它。如果它不存在,抛出一个错误,如果它确实存在,继续前进

/用户的etc/passwd home dir列表

cut -d":" -f6 /etc/passwd | grep home | sort
来自/主页的用户列表

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目录。所以我想知道还有哪些/home目录没有/etc/passwd条目

这是对T起作用的

用于/home/*中的名称;做
如果[-d“$name”];然后
cut-d':'-f6/etc/passwd | egrep-q“^$name$”
如果[$?-ne 0];然后
echo“目录$name与有效用户不对应”
fi
fi
完成

从现在起,我们将开始跑步

userdel-r登录

作为第一近似值:

perl -F: -lane 'next if m/^#/;print "$F[5] for user $F[0] missing\n" unless(-d $F[5])' /etc/passwd
如果要查找
/etc/passwd
/home
之间的差异

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

comm因此,假设您想知道/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$"

这将报告
/etc/passwd
中应位于
/home
但不在以下位置的所有主目录:

cut -d":" -f6 /etc/passwd | grep home | sort | 
    while read dir; do [ -e "$dir" ] || echo Missing $dir; done
这个报告了所有不存在的东西:

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

不完全可靠。帐户通常在/home中有其homedir,但不是所有帐户。例如考虑根帐户,它通常位于
/root
@MarcB中,除非OP想知道/home下是否有目录与/etc/passwd中的条目不对应。您是否担心
/home
中的目录被
/etc/passwd
中的多个条目所占用?这是在AIX上的…perl,在AIX7.1上使用-E,但不是在6.1上。@user2341048现在perl应该可以工作了(使用
-lane
)+1。作为旁注,您还可以将/home/*中的name简化为
;do[-d“$name”]&&cut-d':'-f6/etc/passwd | grep q“^$name$”| | echo“目录$name不对应于有效用户”;完成
。我也会使用简单的
grep
,这里没有必要使用
egrep
,而且,不管怎样,这已经被弃用了(至少对于GNU
grep
)好几年了,取而代之的是
grep-E
。@terdon所有的优点(好吧,除了“弃用的”部分-让我感觉老了)。告诉我:)。我已经养成了使用
grep-E
的习惯,因为手册页建议使用它。POSIX 2008标准化了
-E
选项;不再有单独的
egrep
(或
fgrep
)@JonathanLeffler它们仍然存在于许多(所有?)Linux系统上,以实现向后兼容性。
cut -d":" -f6 /etc/passwd | while read dir; do 
    [ -e "$dir" ] || echo Missing $dir
done