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

Create a script with the following functionality: a. The script takes a list of

ID: 3831301 • Letter: C

Question

Create a script with the following functionality:

a.   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.   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.   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 contain list of users with one user each line. In the same way you can create another file containing list of users(one user per line) and pass the input to script as mentioned above. Also ensure the script has got execute permission(chmod +x rem_usr.sh)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote