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 |
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).
|
Code:
CNT=$(ps -ef $PID -m -o THREAD | wc -l) 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. |
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 )) |
Quote:
Thanks, this method worked. |
Quote:
|
Quote:
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 |
And what if it should be equal to 2000?
|
Quote:
|
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:
|
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 :) ) |
Quote:
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 |
Either add fi for the first if or get rid of the ifs:
Code:
(( $CNT > 2000 )) && echo $CNT || echo "0" |
Quote:
That seems to work..Thanks!! |
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. |