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

Linux: Bash shell script Build a simple script to assist in retrieving user info

ID: 3846861 • Letter: L

Question

Linux: Bash shell script

Build a simple script to assist in retrieving user info from SLASH/etc/passwd and SLASH/etc/group to make user administration simpler.

Build your script so it takes the supplied first name of a user and returns the indicated output. The script should retrieve information for the user the script is run against and an error message if no user is found. Users to test will be Bob, Henry, and Notaperson. Bob and Henry should return the correct information and Notaperson should return an error message of no such person found (as there is no Notaperson user).

To run script input format:

sh userlookup.sh bob

sh userlookup.sh henry

sh userlookup.sh notaperson

Expected output format (will differ based on the user and will display an error when a user is not found):

bob:x:1001:1001:Ops:/home/bob:/bin/bash

bob:x:1001:

ops:x:1004:bob, franks

I need the entire script code please, I am very new to Linux and have tried everything.

Explanation / Answer

Code:
#!/bin/bash   
  
# program to look user in /etc/passwd and /etc/group and report if found otherwise error
  
# validating if there is atleast one argument passed
if [ $# -ne 1 ]   
then
echo "One argument must be passed. Usage: $0 <firstname>"   
exit
fi
  
fname=$1
found=0   
  
# checking if first name is available in /etc/passwd and /etc/group files   
grep -h $fname /etc/passwd /etc/group   
# checking the exit status of grep command and setting the flag variable found to 1   
if [ $? -eq 0 ]   
then
found=1   
fi
  
# verifying if the first name is available in passwd or group files   
if [ "$found" -eq 0 ]   
then
echo "No such person found in /etc/passwd and /etc/group file"
fi

Execution and output:
sh-4.3$ ./userlookup.sh games   
games:x:12:100:games:/usr/games:/sbin/nologin   
games:x:20:   
sh-4.3$ ./userlookup.sh root
root:x:0:0:root:/root:/bin/bash   
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:   
sh-4.3$ ./userlookup.sh adm   
adm:x:3:4:adm:/var/adm:/sbin/nologin
adm:x:4:
sh-4.3$ ./userlookup.sh krishna   
No such person found in /etc/passwd and /etc/group file   

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