Last active
January 9, 2019 13:52
-
-
Save pirafrank/a1de0dae0c201766dddd to your computer and use it in GitHub Desktop.
List logged users and how many sessions they have opened.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HOW TO
Download and make it ready for execution
Run it
$ ./sc1.sh
or just print sessions for users starting with a particular letter, e.g. 'f'
$ ./sc1.sh f