The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM



View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #3 (permalink)  
Old 01-31-2008
Karthikeyan_113 Karthikeyan_113 is offline
Registered User
 

Join Date: Jan 2007
Location: Boston, USA
Posts: 16
Thumbs up Try this....

Hi

Below is the piece with which you can achieve your task.

Code:
while [[ -z $user ]]
do
echo "Enter the user name : "
read user
done

echo
echo "Validating the $user ..."
if [[ `grep -c $user /etc/passwd` -eq 0 ]]
then
   echo
   echo "ERROR : PLEASE ENTER A VALID USERNAME."
   echo "Exiting ..."
   exit 1
else
   echo
   echo "$user IS A VALID USER."
   echo "Checking whether the $user is logged on or not ..."
   echo
fi

if [[ `who | grep -c $user` -eq 0 ]]
then
   echo
   echo "$user IS NOT LOGGED INTO THE SERVER !!!"
   echo
   exit 0
else
   echo
   echo "$user IS LOGGED INTO THE SERVER !!!"
   echo
fi
Below are its execution steps.

Quote:
$ > check_user.sh
Enter the user name :
root

Validating the root ...

root IS A VALID USER.
Checking whether the root is logged on or not ...


root IS NOT LOGGED INTO THE SERVER !!!

$ >
==========
Thanks,
Karthikeyan.
==========
Reply With Quote