Write a Bash shell script “userInfo” that does the following: Asks the user to e
ID: 3764474 • Letter: W
Question
Write a Bash shell script “userInfo” that does the following:
Asks the user to enter the user ID.
Read file “passwdSample.txt” (below) file in the present working directory
Displays: login Name , user name, uid, gid, pathname for user’s home directory, and shell
A line format in the passwdSample.txt file looks as follows:
Where,
Username: It is used when user logs in. It should be between 1 and 32 characters in length.
Password: An x character indicates that encrypted password is stored in /etc/shadow file.
User ID (UID): Each user must be assigned a user ID (UID). UID 0 (zero) is reserved for root and UIDs 1-99 are reserved for other predefined accounts. Further UID 100-999 are reserved by system for administrative and system accounts/groups.
Group ID (GID): The primary group ID (stored in /etc/group file)
User ID Info: The comment field. It allow you to add extra information about the users such as user's full name, phone number etc. This field use by finger command.
Home directory: The absolute path to the directory the user will be in when they log in. If this directory does not exists then users directory becomes /
PasswdSample.txt
Administrator:*:500:513:U-nazeer-PCAdmin:/home/Administrator:/bin/bash
Guest:*:501:513:U-nazeer-PCGuest:/home/Guest:bin/bash
nazeer:*:1000:513:U-nazeer-PC azeer:/home/nazeer:/bin/sh
Sample execution of your program:
%userInfo nazeer
Login name: nazeer
User Information: U-nazeer-PC azeer
Uid: 1000
Gid: 513
Home directory: /home/nazeer
Shell: /bin/sh
Explanation / Answer
# cat get_user_info.sh #!/bin/bash # # Script Name: get_user_info.sh # http://www.thegeekscope.com/script-to-get-linux-user-info/ # # This script is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # # Copyright www.thegeekscope.com # Author: Kam Aggarwal, (kam@thegeekscope.com) # Read the terms of use at http://www.thegeekscope.com/terms-of-use/ # if [ "$1" = "" ] then echo echo "Usage: $0 USERNAME" echo echo "Example: $0 kam" echo exit 1 fi Username=`cat /etc/passwd | grep -Ew ^$1 | cut -d":" -f1` if [ "$Username" = "" ] then echo "Username $1 doesn't exist" exit 2 fi Userid=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f3` UserPrimaryGroupId=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f4` UserPrimaryGroup=`cat /etc/group | grep :"$UserPrimaryGroupId": | cut -d":" -f1` UserInfo=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f5` UserHomeDir=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f6` UserShell=`cat /etc/passwd | grep -Ew ^$Username | cut -d":" -f7` UserGroups=`groups $Username | awk -F": " '{print $2}'` PasswordExpiryDate=`chage -l $Username | grep "Password expires" | awk -F": " '{print $2}'` LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk -F": " '{print $2}'` AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{print $2}'` HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'` echo printf "%-25s : %5s [User Id - %s] " "Username" "$Username" "$Userid" printf "%-25s : %5s " "User Info" "$UserInfo" echo printf "%-25s : %5s [Group Id - %s] " "User's Primary Group" "$UserPrimaryGroup" "$UserPrimaryGroupId" printf "%-25s : %5s " "User is Member of Groups" "$UserGroups" echo printf "%-25s : %5s [Size Occupied - %s] " "Home Directory" "$UserHomeDir" "$HomeDirSize" printf "%-25s : %5s " "Default Shell" "$UserShell" echo printf "%-25s : %5s " "Last Password Changed On" "$LastPasswordChangeDate" printf "%-25s : %5s " "Password Expiry Date" "$PasswordExpiryDate" printf "%-25s : %5s " "Account Expiry Date" "$AccountExpiryDate" echo
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.