사용자 수를 묻는 스크립트는 홈 디렉터리에 있는 파일 수를 확인하고 해당 사용자 목록을 표준 출력으로 보냅니다.

사용자 수를 묻는 스크립트는 홈 디렉터리에 있는 파일 수를 확인하고 해당 사용자 목록을 표준 출력으로 보냅니다.

내 숙제에는 내가 필요해

  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(이 역시 좋은 가정은 아닙니다). 두 번째 가설로 넘어가겠습니다. 그런 다음 다음과 같은 것을 시도해 볼 수 있습니다.

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

방금 두 가지 가정을 더 했습니다. 사용자의 파일 이름에 캐리지 리턴 문자가 포함되어 있으면 카운트가 꺼집니다. 또한 notify여기의 명령은 일종의 이메일이라고 가정 하고 사용자의 이메일이 사용자 이름과 동일하다고 가정합니다. 어쩌면 좋은 가정일 수도 있지만 항상 옳은 것은 아닙니다.

관련 정보