Thursday 29 March 2018

Shell script to display the number of users logged-in

#Display the number of logged in users and the first logged in user info
count=`who|cut -d ' ' -f1|sort -u|wc -l`
echo "Number of users logged in: $count"

echo "First login user details"
echo "------------------------"
fu=`who|head -n 1|cut -d ' ' -f1`
echo "User name = $fu"
echo "Logged in Terminal = `who|head -n 1|tr -s ' '|cut -d " " -f2`"
echo "Log in date = `who|head -n 1|tr -s ' '|cut -d ' ' -f3`"
echo "Log in time = `who|head -n 1|tr -s ' '|cut -d ' ' -f4`"


OUTPUT
csc@csc-SVE1513CYNB ~ $ sh logcnt.sh
Number of users logged in: 1
First login user details
-------------------------------------
User name = csc
Logged in Terminal = tty7
Log in date = 2018-03-28
Log in time = 20:11

No comments:

Post a Comment