LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with wc -l within a script. (https://www.linuxquestions.org/questions/programming-9/help-with-wc-l-within-a-script-897019/)

Q_Linux 08-12-2011 03:31 PM

Help with wc -l within a script.
 
Hello, my script looks like this:

ps -ef $PID -m -o THREAD | wc -l

How do I flag the results to specific numbers. In other words, I only want it to print the output on screen or in a file if the results are only "0" or 2000 and greater. Right now if I run the script it will display the actual processes.

./testpid

1467

Thanks for any help on this

thesnow 08-12-2011 03:50 PM

I'm not sure exactly what you're trying to do, but you could use an if statement to conditionally write/display your result based on a value and range ( = 0 OR is >= 2000).

MensaWater 08-12-2011 04:00 PM

Code:

CNT=$(ps -ef $PID -m -o THREAD | wc -l)
if [ $CNT -eq 0 -o $CNT -ge 2000 ]
then echo $CNT
fi

The first line sets variable, $CNT, to be equal to your wc -l output.

The second line tests the value against 0 (-eq) and (-o = or) against values greater than or equal to 2000 (-ge). It only ouputs the value if it meets one of those conditions.

By the way your ps syntax looks wrong to me but presumably you've got that working already.

grail 08-13-2011 01:48 AM

MensaWater has the good oil, but as a slight alternative on format may I also suggest the following for the if:
Code:

if (( CNT == 0 || CNT >= 2000 ))

Q_Linux 08-14-2011 12:28 AM

Quote:

Originally Posted by MensaWater (Post 4441227)
Code:

CNT=$(ps -ef $PID -m -o THREAD | wc -l)
if [ $CNT -eq 0 -o $CNT -ge 2000 ]
then echo $CNT
fi

The first line sets variable, $CNT, to be equal to your wc -l output.

The second line tests the value against 0 (-eq) and (-o = or) against values greater than or equal to 2000 (-ge). It only ouputs the value if it meets one of those conditions.

By the way your ps syntax looks wrong to me but presumably you've got that working already.



Thanks, this method worked.

Q_Linux 08-14-2011 12:29 AM

Quote:

Originally Posted by grail (Post 4441543)
MensaWater has the good oil, but as a slight alternative on format may I also suggest the following for the if:
Code:

if (( CNT == 0 || CNT >= 2000 ))

Thanks, i'll give this one a try.

Q_Linux 08-15-2011 09:47 AM

Quote:

Originally Posted by MensaWater (Post 4441227)
Code:

CNT=$(ps -ef $PID -m -o THREAD | wc -l)
if [ $CNT -eq 0 -o $CNT -ge 2000 ]
then echo $CNT
fi

The first line sets variable, $CNT, to be equal to your wc -l output.

The second line tests the value against 0 (-eq) and (-o = or) against values greater than or equal to 2000 (-ge). It only ouputs the value if it meets one of those conditions.

By the way your ps syntax looks wrong to me but presumably you've got that working already.

Hi Mensa, I received some further clarification and this is what I need the output to come out as. I want to add an ‘else’ statement to output 0 at all other times, just to be sure we get the exit 0 for SiteScope to evaluate.

Meaning anything else less than 2000 should come up as only a value as 0.

I tried this, but it keeps erring out. Perhaps I am doing something wrong with my scripting.

#!/bin/sh

CNT=$(ps -ef $PID -m -o THREAD | wc -l)

if (( $CNT > 2000 ))
then echo $CNT

if (($CNT < 2000 ))
then echo "0"


fi

grail 08-15-2011 10:05 AM

And what if it should be equal to 2000?

Q_Linux 08-15-2011 10:09 AM

Quote:

Originally Posted by grail (Post 4443531)
And what if it should be equal to 2000?

Just a value count. Anything less than 2000 should have a "0" value. Above 2000, a value count.

ta0kira 08-15-2011 10:32 AM

I don't know about anyone else, but -o THREADS causes an error for me and everything after the first non-option (i.e. $PID) is taken as a pid.
Quote:

$ ps --version
procps version 3.2.8
Kevin Barry

grail 08-15-2011 10:40 AM

So then you have what you need ... unless there is another question that I cannot see??

@Kevin - I never bothered to test OPs ps because he seems to imply that it works for him and the arithmetic seemed to be the difficulty (could be wrong of course :) )

Q_Linux 08-15-2011 10:50 AM

Quote:

Originally Posted by grail (Post 4443556)
So then you have what you need ... unless there is another question that I cannot see??

@Kevin - I never bothered to test OPs ps because he seems to imply that it works for him and the arithmetic seemed to be the difficulty (could be wrong of course :) )


Yes, the o THREADS works internally for me. What I do need is a way to push a value of 0 if the count is below 2000. Unfortunately it gives me an eror.

0403-057 Syntax error at line 6 : `then' is not matched.

Script

$ cat testpid1
#!/bin/sh

CNT=$(ps -ef $PID -m -o THREAD | wc -l)

if (( $CNT > 2000 ))
then echo $CNT

if (($CNT < 2000 ))
then echo "0"


fi

ta0kira 08-15-2011 10:53 AM

Either add fi for the first if or get rid of the ifs:
Code:

(( $CNT > 2000 )) && echo $CNT || echo "0"
Kevin Barry

Q_Linux 08-15-2011 10:56 AM

Quote:

Originally Posted by ta0kira (Post 4443568)
Either add fi for the first if or get rid of the ifs:
Code:

(( $CNT > 2000 )) && echo $CNT || echo "0"
Kevin Barry


That seems to work..Thanks!!

grail 08-16-2011 12:48 AM

This will include 2000 which you implied was not to be included in post #9


All times are GMT -5. The time now is 03:13 AM.