Skip to content

Instantly share code, notes, and snippets.

@pirafrank
Last active January 9, 2019 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pirafrank/a1de0dae0c201766dddd to your computer and use it in GitHub Desktop.
Save pirafrank/a1de0dae0c201766dddd to your computer and use it in GitHub Desktop.
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
Copy link
Author

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