I know it's an amateur question. I need to write a Bash shell script which count how many users are online at the moment on the SSH server. I need to ignore multiple logins and show the username and the place where they logged in.
This smells like homework but oh well:
First we builds a list of logged in users with the "user" command, and filter out duplicates by putting one on each line (xargs -n) and sorting with uniques filtered (sort -u).
w -h prints a list of all logged in users, we pipe that through awk printing only the desired fields (1 and 3), piping the result through grep to filter all but the user we're looking for on each loop iteration. -m1 ensures it stops after the first match, you could also pipe through 'head -1' instead.
Same question was asked on stackoverflow. Not sure what the procedure is, but I guess I'll re-post my answer To get unique list of users with some login across the network
To get count
And same thing with