Link to home
Start Free TrialLog in
Avatar of Shamsul Kamal
Shamsul Kamal

asked on

How to count output lines in PERL ?

Hi,

I would like to request an assistant.

May i know how to count how many line for output ?

I would like to accomplish the following :

if output from "ps aux" run from shell having MORE than 300 lines
{
email to root
}
if output from "ps aux" run from shell having LESS than 300 lines
{
do something else
}

Thank you.
Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

can you redirect the output to a file and then do a  wc -l filename to get the number of lines.
Avatar of Shamsul Kamal
Shamsul Kamal

ASKER

If possible i would like to have this on the fly (without writing to a file) as to avoid high CPU consumption.
Avatar of FishMonger
How are you executing "ps aux"; via system(), or backticks, or a piped open?

You could put the output into an array and the number of lines will be the size of the array.
I actually wanted to create a perl script and trigger it using crond from shell .

I have root access to the server.

The ps aux is actully run directly from the system shell.

ASKER CERTIFIED SOLUTION
Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
#!/usr/bin/perl
if( (()=`ps aux`) > 300 ){
email to root
}else{
do something else
}