ユーザー数を求めるスクリプトは、ホームディレクトリ内のファイル数を確認し、そのユーザーのリストを標準出力に送信します。

ユーザー数を求めるスクリプトは、ホームディレクトリ内のファイル数を確認し、そのユーザーのリストを標準出力に送信します。

私の宿題には私が必要です

  1. ユーザーのホームディレクトリにあまりにも多くのファイルが保存されており、最初の5人の違反者に知らせたいと思います。より多くのユーザーまたはより少ないユーザーに対してこのスクリプトを再実行できるため、この選択は識別する必要があるユーザーの数を尋ねるメッセージを表示し、ホーム・ディレクトリー内のファイル数を確認し、そのユーザーのリストを標準出力に送信します。 。 。

これは私が今までに得た最初の部分です。 #5は不明です。

echo " Please enter file name: "
read workingfile 
while true
 do
  echo "1)  Check who is logged into the system and pull up contact information"
  echo "2)  Check who has left a system process running and stop it"
  echo "3)  Check if two users have the same userid, and prompt to change it"
  echo "4)  Get a list of all users on file and list information about them"
  echo "5)   List the top 5 users that have to many files in their home directory and list them"

  echo " "
  echo -n "Choice: "
  read choice
  case "$choice" in

1)
      user=$(who | cut -d' ' -f1 | sort | uniq)
     grep "$user" $workingfile | sed 's/\:/ /g' | sed 's/stu[0-9]*//g'
     break
        ;;

2)
    echo "Please enter a username: "
    read user
    echo $user
    ps -u $user
    echo "Would you like to end the proccess for $user ? (yes or no)"
    read choice2
        if [ $choice2 = "yes" ];
                echo "killall -u USERNAME.”             break 
        else 
            echo "We will not be stopping any background proccesses!"
            break 
        exit
        fi
    ;;

3)
    sed -i '0,/stu5/{s/stu5/stu6/}' myuser.txt 
    sed -i '0,/stu5/{s/stu5/stu4/}' myuser.txt 
        echo "Testing if a user has the same id"
        if [[ $(grep -o "stu[0-9]" $workingfile | uniq -d) != 0 ]]; then
            result=$(grep -o "stu[0-9]" $workingfile | uniq -d)
            echo " We will be replacing the first instance of $result please input what number you'd like to replace it with!: "
            read input
            echo "replacing id..."
            sed -i '0,/stu5/{s/stu5/stu4/}' $workingfile
            cat $workingfile
            break
        else
            echo " There is no result!"
            break
         exit
         fi
    ;;

4) echo "List of all users in file: "
        cat $workingfile | sed 's/\:/ /g' | sed 's/stu[0-9]*//g'
        break

    ;;

ベストアンサー1

getent passwdまず、すべての有効なユーザーが実際に返されるか(場合によっては返されない)、すべてのユーザーのホームディレクトリが形式に従うと仮定する必要があります/home/username(これも良い仮定ではありません)。 2番目の仮説に進みます。その後、次のことを試すことができます。

cd /home
for user in *; do printf "%s\t%s\n" $(find $user -type f | wc -l) $user; done | sort -rn > /tmp/sort.$$
for user in $(head -5 /tmp/sort.$$ | cut -f2); do notify $user; done

2つの仮定を追加しました。ユーザーのファイル名にキャリッジリターン文字が含まれていると、カウントはオフになります。また、notifyここのコマンドは一種の電子メールであると仮定し、ユーザーの電子メールがユーザー名と同じであると仮定します。たぶん良い家庭かもしれませんが、必ずしも正しいとは限りません。

おすすめ記事