Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
List logged users and how many sessions they have opened.
#!/bin/bash
if [[ $# > 1 ]]; then
echo "Error: wrong number of arguments"
exit -1
else
if [[ $1 == "" ]]; then
while read -r line
do
USER=$(echo $line | awk '{print $2}')
TIMES=$(echo $line | awk '{print $1}')
echo "$USER is logged $TIMES times."
done < <(w | sed '1,2d' | cut -f1 -d' ' | sort | uniq -c)
else
FIRST_LETTER=$1
while read -r line
do
USER=$(echo $line | awk '{print $2}')
TIMES=$(echo $line | awk '{print $1}')
echo "$USER is logged $TIMES times."
done < <(w | grep "^$FIRST_LETTER" | cut -f1 -d' ' | sort | uniq -c)
fi
fi
@pirafrank

This comment has been minimized.

Copy link
Owner Author

commented Dec 31, 2015

HOW TO

Download and make it ready for execution

$ cd $HOME && wget http://bit.ly/1Ui4pbP -O sc1.sh && chmod +x sc1.sh

Run it

$ ./sc1.sh

or just print sessions for users starting with a particular letter, e.g. 'f'

$ ./sc1.sh f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.