Open a pipe to the who command
#!/usr/bin/perl
use strict;
my %who;
open (WHOFH,"who |") or die "Can't open who: $!";
while (<WHOFH>) {
next unless /^(\S+)/;
$who{$1}++;
}
foreach (sort {$who{$b}<=>$who{$a}} keys %who) {
printf "%10s %d\n",$_,$who{$_};
}
close WHOFH or die "Close error: $!";
Related examples in the same category