Thursday, 4 April 2019

Shell script to read a username and display how many times a user has logged in.

#Counting the number of times a user has logged in
echo "Enter the username"
read uname
times=`who | grep  "$uname" | wc -l`
if [ $times -ne 0 ]
then
   echo "$uname has logged in $times times"
else
   echo "user [$uname] not logged in"
fi

 
OUTPUT
Safi>sh eb.sh
Enter the username
csc
csc has logged in 3 times
Safi>sh eb.sh
Enter the username
sss
user [sss] not logged in