show user's info

#!/bin/bash

# display user's info...

/bin/echo "Please input the username"
read username
/bin/grep $username /etc/passwd > /dev/null 2> /dev/null
if [ $? -eq 0 ]
then
         /bin/echo "username is : $username"
else
         /bin/echo "user $username does not exist"
fi
/bin/echo
# list /etc/passwd info
userinfo=`/bin/grep ^$username:x /etc/passwd`
userid=`/bin/echo $userinfo | /bin/awk -F : '{print $3}'`
groupid=`/bin/echo $userinfo | /bin/awk -F : '{print $4}'`
homedir=`/bin/echo $userinfo | /bin/awk -F : '{print $6}'`
shell=`/bin/echo $userinfo | /bin/awk -F : '{print $7}'`

# get group name from GID
grouptmpname=`cat /etc/group | /bin/grep :x:$groupid`
groupname=`/bin/echo $grouptmpname | /bin/awk -F : '{print $1}'`
/bin/echo "user id is : $userid"
/bin/echo "default group is : $groupname"
/bin/echo "home directory is : $homedir"
/bin/echo "shell is : $shell"
/bin/echo "group members info :"
# get group members
groups=`/usr/bin/groups $username`
/bin/echo "$groups"
/bin/echo

#get login info
userlogin=`/usr/bin/who | /bin/grep $username`
if [ "$userlogin" != "" ]
then
     /bin/echo "$username is online"
else
     /bin/echo "$username NOT logged in"
fi

你可能感兴趣的:(shell)