C Shell script to count current number of users

Where Penguins and Daemons chill together in the warmth of the Sun.

Moderators: SecretSquirrel, notfred

C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 12:47 pm

Hey guys, I've been experimenting with some C shell scripting and playing with things like pipes and put together simple text based menus. I'm looking to make a C shell script that will run without asking any input from the user and then count the number of users logged onto the server at that particular moment (like a who | wc). It will do this a preset number of times with a 1min gap between each inquiry. After the preset number of runs (I'm guessing a loop is to be used) it will print the lowest numbers of users logged onto the server during the script run.

I'm looking for something that appears this way to the user:

Cycle 1 : number of users = 12
Cycle 2 : number of users = 12
Cycle 3 : number of users = 11
Cycle 4 : number of users = 10
Cycle 5 : number of users = 12
Cycle 6 : number of users = 11
Cycle 7 : number of users = 13
Cycle 8 : number of users = 12
Cycle 9 : number of users = 14
Cycle 10 : number of users = 16
Cycle 11 : number of users = 13
Cycle 12 : number of users = 13
Cycle 13 : number of users = 12
Cycle 14 : number of users = 13
Cycle 15 : number of users = 13

The smallest number of users: 10
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 1:38 pm

I've got this so far:

#!/bin/csh
# file: usercount
# Count number of users logged onto the user once every minute for fifteen minutes,
# then print lowest number of users over the course of the past 15 minutes

@ x = 1
set y = `who | wc -l`

while ( $x <= 15 )
echo "Cycle $x : The number of users is $y"
@ x++
sleep 60
end

_______

How do I go about taking the lowest user count over the 15 cycles and printing it back to the screen?
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 2:58 pm

Have another variable that you call lowest and initialise at start to how many are currently on or to MAXINT or similar . After that, compare if y<lowest and if so set lowest=y.
notfred
Grand Gerbil Poohbah
 
Posts: 3742
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 3:02 pm

notfred wrote:Have another variable that you call lowest and initialise at start to how many are currently on or to MAXINT or similar . After that, compare if y<lowest and if so set lowest=y.


Would it be possible for you to write a sample script? What you're saying sounds right but I'm not sure how it can be integrated into my current script.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 7:14 pm

Another flaw in my script is that by setting y to who | wc -l, I'm feeding the same value for y during the entire course of the script even if the actual server user count is changing.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 8:27 pm

My intent is not to rain on your parade, but this should be required reading: http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

Most of it probably will not make sense to you right now, but suffice it to say, learn Bourne or Korn for your scripting. The sys-admins of the world will be kinder to you. :)

Notfred gave a pretty good hint on the tracking the lowest number of users. I will give one more, which dmitriylm hinted at. You must update your variables within the loop.


--SS
SecretSquirrel
Gerbil Jedi
Gold subscriber
 
 
Posts: 1719
Joined: Tue Jan 01, 2002 7:00 pm
Location: The Colony, TX (Dallas suburb)

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 8:43 pm

Another thing to note -- which may or may not matter (depending on what you're trying to do) -- is that "who | wc -l" will give you the number of ttys being used, not the number of distinct users. For example, my system has only one true user, but "who | wc -l" says 40. "who | cut -f1 -d' ' | sort -u | wc -l" will give a count of unique user names logged-on (you could also do "who -q | head -n1 | xargs -n1 | sort -u | wc -l"). Of course, you may want to count ttys.

I'd also echo SS's call for using Bourne scripting rather than csh. ksh is pretty useful if you want to be portable across a wide range of Unix-like systems.
bitvector
Grand Gerbil Poohbah
 
Posts: 3234
Joined: Wed Jun 22, 2005 4:39 pm
Location: Mountain View, CA

Re: C Shell script to count current number of users

Postposted on Tue Apr 21, 2009 9:21 pm

dmitriylm wrote:Would it be possible for you to write a sample script? What you're saying sounds right but I'm not sure how it can be integrated into my current script.
Sorry but this is way too much like homework for me to do it for you.

I'll also echo what others have said about not using C-shell for scripting, but I assume that you don't get to choose that part of the assignment. :)
notfred
Grand Gerbil Poohbah
 
Posts: 3742
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 12:23 am

Not homework but my implementation of several small guides on scripting from a unix/linux guide book. If it bothers you so much I certainly can't force you to help me.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 7:54 am

dmitriylm wrote:Not homework but my implementation of several small guides on scripting from a unix/linux guide book. If it bothers you so much I certainly can't force you to help me.

No, no, we'll help you, but if you're trying to learn it, we won't spoon feed you either.

Bear in mind, that there have been a rash of people who will post their homework assignment on the boards here and expect us to do the work for them. So, you can understand our hesitation to do much more than point you in a certain direction.

On that note, I'm not much of a programmer, but I can give you a little pseudo-code that will nudge you in the right direction.
Code: Select all
# If a variable must be initialized, give it a ridiculously high value for the function it will serve.
mincount = 10000
# This variable for the counter of the while loop
x = 0
# This variable will hold the user input for how many times to run the loop.
y = <userinput>
# Start a loop and have it run for however long the user wants it to.
while (x < y) {
  z = number of users currently logged on
  # Conditional statement to see if z is less than mincount, and if it is, overwrite mincount with z.
  if (z < mincount) {
    mincount = z
    }
# Increment x otherwise you'll be stuck with an infinite loop.
x++
}
# Now print the mincount
print mincount


You seem to know the actual code you have to write, but this should help you get going in the right direction.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
titan
Grand Gerbil Poohbah
 
Posts: 3276
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 9:32 am

You might get more offers of help if you were working in a scripting language that hasn't been deprecated for a number of years. Hardly anyone uses C shell anymore...
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37831
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 12:00 pm

just brew it! wrote:You might get more offers of help if you were working in a scripting language that hasn't been deprecated for a number of years. Hardly anyone uses C shell anymore...


Point taken, but its easy enough to understand and get my bearings straight before jumping into more difficult languages. Thanks a lot for the help guys, I think setting the min count to a value and checking the current user value against the min count to set a new min count is going to work well.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 12:36 pm

Here is the finished product, I can't believe I was missing something so simple. Now onto more difficult things!

#!/bin/csh
# file: usercount
# Count number of users logged onto the user once every minute for fifteen minutes,
# then print lowest number of users over the course of the past 15 minutes

@ x = 1
@ mincount = 9999

while ( $x <= 15 )
set y = `who | wc -l`
echo "Cycle $x : The number of users is $y "
@ x++
sleep 60

if ( $y < $mincount) then
@ mincount = $y
endif

end

echo " "
echo "The lowest user count is $mincount"
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 12:56 pm

Do you need to filter duplicate entries for the same user? You may want to implement bitvector's change to the line that calculates the number of current users.
(this space intentionally left blank)
just brew it!
Administrator
Gold subscriber
 
 
Posts: 37831
Joined: Tue Aug 20, 2002 10:51 pm
Location: Somewhere, having a beer

Re: C Shell script to count current number of users

Postposted on Wed Apr 22, 2009 12:58 pm

just brew it! wrote:Do you need to filter duplicate entries for the same user? You may want to implement bitvector's change to the line that calculates the number of current users.


You are correct, I will implement that as well.
dmitriylm
Graphmaster Gerbil
 
Posts: 1054
Joined: Thu Dec 27, 2001 7:00 pm
Location: Bay Area/Cali


Return to Linux, Unix, and Assorted Madness

Who is online

Users browsing this forum: No registered users and 2 guests