Click to See Complete Forum and Search --> : C Shell, display user count 15 times then display lowest count


dmitriylm
April 21st, 2009, 05:07 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

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?

PeejAvery
April 22nd, 2009, 11:39 AM
Just create a variable named $lowest = 999. Then in each section of the while loop, check to see if $y is less than $lowest. If it is, set $lowest = $y.

dmitriylm
April 22nd, 2009, 01:50 PM
Just create a variable named $lowest = 999. Then in each section of the while loop, check to see if $y is less than $lowest. If it is, set $lowest = $y.


You are a genius! I don't know why I didnt think of that. Finished product:

#!/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"