Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX \"Desbian System\" commands please and thank you very much! Create a script

ID: 3831336 • Letter: U

Question

UNIX "Desbian System" commands please and thank you very much!

Create a script with the following functionality: a. (3 points) The script takes a list of users from standard in. Note: this implies the script will be executed as follows: cat /root/users | ./script.bash and the file that contains a list of users is a list with a one user per line b. (3 points) Before removing each user, you need to ensure the user exists (hint: use this $(cat /etc/password | egrep “^$username”) and see if it is an empty string) c. (3 points) If the user exists, remove the user and force the deletion of all directories

Explanation / Answer

Code:
#!/bin/bash

# script to remove list of given users

# Reading input from stdin
while read usr
do
   # Executing grep command to check if user exists in /etc/passwd file
   g_out=$(grep "^$usr" /etc/passwd)

   # g_out variable is empty when the user does not exist
   # g_out variable will have some value when the user exist in /etc/passwd file
   if [ "$g_out" != "" ]
   then
       # displaying user exist message
       echo "User $usr exist"

       # deleting user along with files in the system owned by user forcibly
       deluser --force --remove-all-files $usr      
   else
       # displaying user does not exist message if user does not exist in /etc/passwd file
       echo "User $usr does not exist"
   fi
done

Execution and output:
The script will display "user does not exist" message if user is not available in /etc/passwd file. Below is an execution example. Otherwise the script will display "User exist" message and removes the user and files owned by the user.

186590cb0725:Shell bonkv$ cat /tmp/usr
krishna
186590cb0725:Shell bonkv$ cat /tmp/usr|./rem_usr.sh
User krishna does not exist

NOTE: /tmp/usr will have list of users where each line contains one username. The same way you can provide the list of users in a file and pass it to the script like above.