Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

who ¦ wc -l

Status
Not open for further replies.

MagnumVP

IS-IT--Management
Jul 9, 2002
109
US
I'm looking for a script that would echo a list of # of users.

Such as "Currently 10 Users" or "Currently 1 User".

The difference is grammer. The (s) in user(s) whould only be there if there are 2 or more users.

I'm writing in SunOS 5.8 in the Korn Shell.

Any Thoughts.

Here is what I have so far;

echo There are $(who | wc -l) Users online (which is not correct)
 
#!/bin/sh
# Script for counting number of users online.
nummy=`who | wc -l`

if [ $nummy = 1 ];
then
echo "There is currently $nummy user online."
else
echo "There are currently $nummy users online."
fi

The following should help you on your way. You'll notice that it will leave the number off center a bit, which can be corrected by running sed. I didn't have the time to look up the syntax, but it should be pretty simple. Hope this helps.

blaine
 
Here is the problem that I'm having. When I type nummy='who | wc -l' then type $nummy I receive a response as if I just typed in the 'who' command.

MagnumVP pts/2 Jul 9 11:09 (Insert Home name Here)

It seems to ignore the '| wc -l'

Any Thoughts?
 
Magnum:

I ran Blaine's script, and it worked. It looks like you are using single quotes instead of back ticks (`) around who|wc -l.

Here's my example and I'm using sed to center. The sed nugget was given to me earlier by Nate, the bigoldbulldog.


#!/bin/sh
# Script for counting number of users online.
nummy=`who | wc -l`
##nummy='who | wc -l'

if [ $nummy = 1 ];
then
echo "There is currently "$nummy" user online." |
sed '
:a
/^.\{1,72\}$/ s// &/
ta
/\( *\)\1/ s//\1/
'
else
echo "There are currently "$nummy" users online."|
sed '
:a
/^.\{1,72\}$/ s// &/
ta
/\( *\)\1/ s//\1/
'
fi


Regards,


Ed
 
MAgnum,

The script mentioned by blaineprutt runs on my workstation as well. Checkout if you r using back quotes while assigning the who|wc-l value to the variable..

Regds,

- Hemant
 
Another way to centre the number is as follows:

nummy=`who | wc -l | tr -s ' ' ' ' | cut -f2 -d ' '`

if [ $nummy = 1 ];
then
echo "There is currently $nummy user online."
else
echo "There are currently $nummy users online."
fi

Again use backticks to contain everything within the who command. Hope this helps.
 
Another way ...

echo Currently $(w|head -1|awk -F, '{print $3}') Online

Greg.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top