From: Dave O Connor (doc at domain redbrick.dcu.ie)
Date: Tue 02 May 2000 - 15:46:00 IST
Someone called ilug-admin at domain linux.ie said on Tue, May 02, 2000 at 03:06:59PM +0100:
> Tina Marie's [TinaDp6 at domain netscape.net] 22 lines of dribble included:
> >
> >
> > I need to have the number of users printed out within my perl script.
> >
> > I know the UNIX command is w -h | wc -l
> That doesn't work if users are logged in multiple times.
> w | awk '{print $1}' | sort | uniq | wc -l
> (sort -u saves a pipe on some systems)
>
> > But how do I get it to print out like the sample below?
> >
> >
> > There are 7 users logged on the system.
> This sounds more of a shell scripty way of doing things than actually needing
> to do it in perl. However if what you're looking for is something like
>
> #!/usr/bin/perl
> $blah = system("w | awk '{print $1}' | sort | uniq | wc -l");
> print "There are $blah users logged on";
>
That'd ouput the return value from that nasty shell command, which you don't
really want. A slightly more efficient (resourcewise, anyway) would be
something like:
sub num_users
{
open(USERS,"/usr/bin/users|");
my $users = <USERS>;
close USERS;
my at domain userlist = split(/\s+/,$users);
my at domain uniqlist;
foreach ( at domain userlist)
{
if (!grep($_, at domain uniqlist))
{
push( at domain uniqlist,$_);
}
}
return scalar at domain uniqlist;
}
- DoC, the bored.
This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:05:59 GMT