シャドウファイルとpasswdファイルを比較するための完全なbashスクリプトをどのように作成できますか?

シャドウファイルとpasswdファイルを比較するための完全なbashスクリプトをどのように作成できますか?
  1. passwdとShadow aという2つのファイルがあります
    。 2つのファイルの並べ替え
    b。 2つのファイルを1行ずつ比較します
      。 i.一致するユーザー名が3番目のファイルとして出力されます
      。 ii。一致するものがない場合は、4番目のファイルに出力
      iii.ユーザーが一致しない場合は、ユーザーがどのファイルにあるかを確認してください。
  2. パスワードまたはシャドウ
  3. passwdファイルにユーザーがいない場合は、/home/test123を確認してください。
  4. また、システム内のホームディレクトリを探します。 /homeの下にない可能性があります。
  5. ホームディレクトリがある場合は、ユーザーを追加します。
  6. ユーザーを追加
  7. ホームディレクトリが存在しない場合は、シャドウファイルからそのエントリを削除します。
  8. userdel(xxx) の使用
  9. ホームディレクトリが/ home以外のファイルシステムにある場合は、ユーザーを作成し、
    ユーザーエントリaのホームディレクトリを使用します。前任者。 /選択/ムラード
  10. 変更が完了したら、比較を再実行する必要があります。
  11. 次に、プログラミングでモジュール(シェルプログラミングではサブルーチンとも呼ばれます)を使用し、Compare_filesというモジュールを作成します。
  12. Compare_filesにパラメータを渡します。
  13. 2 つのモジュールを作成します。
    a. file() を確認します。
      i.一致するものがない場合は空です。
  14. 出口
  15. モジュールサブルーチンの生成

比較してみたができるのはそれだけだ。ついています。

これが私がこれまでにしたことです:

#!/bin/sh
password_file=pass.txt
password_file_sorted="sorted_$password_file"
shadow_file=shadow.txt
shadow_file_sorted="sorted_$shadow_file"
match_file="match_record.txt"
not_match_file="not_match_record.txt"

# empty target file
cp /dev/null $match_file
cp /dev/null $not_match_file

# check username is exist in file
check_file() {
    username=$1
    file=$2
    exit_status=1 # username does not exist in file

    # read line by line
    while IFS=: read -r f1 f2 f3 f4 f5 f6 f7
    do
        if [ "$f1" = "$username" ]; then
            exit_status=0 # username exist in file
        fi
    done <"$file"

    return $exit_status
}

# create module compare_file
compare_file() {
    file_1=$1
    file_2=$2

    # read line by line content of file_1
    while IFS= read -r line
    do
        username=$(echo $line | awk -F: '{print $1}')

        # check username in file_1 is exist on file_2
        check_file "$username" "$file_2"
        if [ $? -eq 0 ]; then 
            #if user exist, write record to match_record.txt file
            target_file=$match_file
        else
            #if user does not exist, write record to not_match_record.txt file
            target_file=$not_match_file
        fi

        echo $line >>$target_file

    done <"$file_1"
}


# short password files
echo "short $password_file"
sort $password_file > $password_file_sorted

# short shadow files
echo "short $shadow_file"
sort $shadow_file > $shadow_file_sorted

# compare file password with shadow
echo "check username in $password_file is exist on $shadow_file" 
compare_file $password_file_sorted $shadow_file_sorted

# compare file shadow with password 
echo "check username in $shadow_file is exist on $password_file" 
compare_file $shadow_file_sorted $password_file_sorted

# add or remove user
echo "for every mismatch username in $password_file, add user if home directory exist and remove user if home directory does not exist"
while IFS= read -r line
do
    username=$(echo $line | awk -F: '{print $1}')
    home_directory=$(echo $line | awk -F: '{print $6}')

    # check user does not exist in passwd
    num_entries_in_password=$(grep $username $password_file | wc -l)
    if [ $num_entries_in_password -eq 0 ]; then

       # check if /home/username directory exist
       if [ -d "/home/$username" ] ; then 
           echo "/home/$username directory exist, add $username with command: useradd $username"
           useradd $username
       elif [ -d $home_directory ]; then 
           # add user with specific user directory
           echo "$home_directory directory exist, add $username with command: useradd $username -b $home_directory"
           useradd $username -b $home_directory
       else
           echo "remove user: $username"
           userdel $username
       fi 
    fi

    echo "user: '$username' exist in $password_file"

done <"$not_match_file"

私が得た結果は非常に短いです。なぜこれが起こるのですか?

ベストアンサー1

このスレッドが死んでいる可能性が高いですが、まだ答えます。
あなたが持っている問題はあなたの問題です。ファイル
から始めて、passwdループ内のユーザーを2つのグループ(配列を使用)のいずれかにソートします。 1つはユーザーIDが1000未満のユーザーのための配列、もう1つの配列はIDが1000以上のユーザーのための配列です。 1000人未満のユーザーはシステムユーザーであり、残りは探しているユーザーです。

別のループでは、システムユーザーをフィルタリングし、シャドウファイルにないユーザーを確認します。

注:使用されるすべての一時ファイルはtmpディレクトリに作成/保存する必要があります。
さらに:シャドウファイルにはルートアクセスが必要です。

おすすめ記事