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

Hello Working on a project, and a bit stuck at the moment, here is what I have t

ID: 3848334 • Letter: H

Question

Hello

Working on a project, and a bit stuck at the moment, here is what I have to do:

   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

Explanation / Answer

Hi there, thanks for asking and thanks for keeping the question nice, short and clear.

#!/bin/sh
# Get uid of the user.
uid=$(id -u ${1})
# Get the length of the uid you got.
len=${#uid}
# UID lengths are usualy 4 long, so if its not zero, usually means you got a user
# by that name.
if [ $len -ne 0 ]:
then
    # concatenate /etc/passwd file then grep any lines with given uid.
    user_data=$(cat /etc/passwd | grep ${uid})
    # Print data.
    echo $user_data
else
    # Throw and additional error.
    echo "OOPs, try some other name!"
fi

Have a great day.

Cheers,

Rj

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