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

最初の近似として:

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    <(
            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 のみにあり、/etc/passwd には含まれていないディレクトリを表示します。/home
  • comm -12 ....- 正しいディレクトリが表示されます ( と にも存在し/etc/passwdます/home)

-{max|min}depthAIXの場合はよくわかりません。

于 2014-10-07T19:44:34.173 に答える
2

/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
于 2014-10-07T19:53:23.823 に答える
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 に答える