Create a shell script to gather user account information and displays the result to administrator

I want to create a shell script to gather user account information and displays the result to administrator.

I have created a script but its showing all the information when i search for username like:

amit@mx:~$ ./uinfo.sh  amit

Username                  :  amit  [User Id - 1000]
User Info                 : amit,,,

User's Primary Group      :  amit  [Group Id - 1000]
User is Member of Groups  : amit adm cdrom sudo dip plugdev lpadmin sambashare

Home Directory            : /home/amit  [Size Occupied - 162M]
Default Shell             : /bin/bash





I want to use script like 



./uinfo.sh -i <username> Display user ID
./uinfo.sh -g <username> Display user GID


===========================================
#!/bin/sh

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": " '$
LastPasswordChangeDate=`chage -l $Username | grep "Last password change" | awk $
AccountExpiryDate=`chage -l $Username | grep "Account expires" | awk -F": " '{p$
HomeDirSize=`du -hs $UserHomeDir | awk '{print $1}'`

echo
printf "%-25s : %5s  [User Id - %s]\n" "Username" "$Username" "$Userid"
printf "%-25s : %5s\n" "User Info" "$UserInfo"
echo
printf "%-25s : %5s  [Group Id - %s]\n" "User's Primary Group" "$UserPrimaryGro$
printf "%-25s : %5s\n" "User is Member of Groups" "$UserGroups"
echo
printf "%-25s : %5s  [Size Occupied - %s]\n" "Home Directory" "$UserHomeDir" "$$
printf "%-25s : %5s\n" "Default Shell" "$UserShell"
echo

=========================================

can anyone help...

Hello!

In case you forgot to read the forum rules, here is quick copy.

1 Like

It would help to know which shell and which OS you are using. As it is my crystal ball with which i used to look into your computer usually is in repair right now.

You have to put in some mechanism to understand commandline options to do that. The usual way is to use getopts to accomplish this. I suggest to read it man page. I have showcased its usage in this thread and script which you might want to use as a starter.

This (and all the similar lines) contains several mistakes at once:

First, this is a so-called "useless use of cat" or "UUOC". The error is so "famous" that it even has its own name (and you can look it up in google): utilities like grep do not need a cat because they can either take input from stdin or a filename themselves. The following lines do absolutely the same but the second variant uses one process less to accomplish it:

cat /some/file | grep "<regexp>"
grep "<regexp>" /some/file

Second, you should NOT use backticks any more, regardless of which shell you use. Even the oldest shell running today understands modern POSIX process substitution:

var=$(process1 | process2 | ... )

and if a shell doesn't: put it in a museum where it belongs but don't use it any more. Get something from this (or at least the last) century to use instead of an pre-bronze-age antiquity.

Third: you read the file /etc/passwd over and over again for each bit of information you look for:

Username=$(cat /etc/passwd | grep ..... )
Userid=$(cat /etc/passwd | grep ..... )
UserPrimaryGroupId=$(cat /etc/passwd | grep ...... )
....

you can get all the info in one pass: you know that /etc/passwd is organised in fields separated by a colon character (":"), yes? You can use the field-splitting ability of the shell by telling it which character to split at. Per default this is a space, but it can be changed by changing the IFS (internal field separator) variable:

IFS=":" read Username junk UserId UserPrimaryGroupId ..... < $( grep "username" /etc/passwd )

This will set all the variables at once: the first field goes to "Username", the second to "junk" (you seem not to use it, i make a habit of naming not-needed info that way), the third one to "UserId", etc.. Be sure to put an additional "junk" at the end of the variable list for any piece of information at the end of the line you are eventually not interested in because all "left over" parts of the input goes to the last variable in the list.

One last word of caution: you use:

grep -Ew ^$user

to select the respective line in /etc/passwd . This may create problems in case you i.e. have two users named "joe" and "joex". You should also protect your script against injections of wildcards - suppose i enter "." as a username to search for. Your grep-command would select any user and your subsequent script (which relies on exactly one user being picked) would break. The following will reduce this risk:

fgrep "^${user}:" /etc/passwd

I hope this helps.

bakunin