Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question
Shell meaning Linux shell? Which linux shell? "Online atm at the server", how do people log in to your server (ssh, telnet, ftp, etc.)? That may change things. – nerdwaller Dec 11 '13 at 21:12
Linux shell, bash shell, SSH logs, i tried with rwho but it show the multiple logins – Patrik Zelena Dec 11 '13 at 21:20
If you're interested in showing the location of each login, perhaps you need to reconsider ignoring multiple logins; if I'm connected via SSH to host A from both host B and host C, you can either show me as logged in twice, once from each host, or throw away some information. – Aaron Miller Dec 11 '13 at 21:38
Jeh, It shows all the hosts, that's why I need to ignore multiple logins and show the first host, not all. – Patrik Zelena Dec 11 '13 at 21:42

This smells like homework but oh well:


users=$(users | xargs -n1 | sort -u)
for u in $users; do
    w -h | awk '{print $1 " " $3}' | grep -m1 $u


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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .