LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-12-2011, 03:31 PM   #1
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Rep: Reputation: 0
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
 
Old 08-12-2011, 03:50 PM   #2
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
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).
 
0 members found this post helpful.
Old 08-12-2011, 04:00 PM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
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.

Last edited by MensaWater; 08-12-2011 at 04:01 PM.
 
Old 08-13-2011, 01:48 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 ))
 
Old 08-14-2011, 12:28 AM   #5
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MensaWater View Post
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.
 
Old 08-14-2011, 12:29 AM   #6
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
 
Old 08-15-2011, 09:47 AM   #7
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MensaWater View Post
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
 
Old 08-15-2011, 10:05 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
And what if it should be equal to 2000?
 
Old 08-15-2011, 10:09 AM   #9
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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.
 
Old 08-15-2011, 10:32 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-15-2011, 10:40 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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 )
 
Old 08-15-2011, 10:50 AM   #12
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
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
 
Old 08-15-2011, 10:53 AM   #13
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Either add fi for the first if or get rid of the ifs:
Code:
(( $CNT > 2000 )) && echo $CNT || echo "0"
Kevin Barry
 
Old 08-15-2011, 10:56 AM   #14
Q_Linux
LQ Newbie
 
Registered: Mar 2011
Location: Miami
Distribution: Suse, RedHat, Ubuntu, CentOS
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ta0kira View Post
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!!
 
Old 08-16-2011, 12:48 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
[SOLVED] Script question: create a shell script in kde to log in on a server with ssh c4719929 Linux - Newbie 1 01-31-2011 03:05 AM
How to get full path to script file inside script itself? And in case of sym links? maggus Linux - Newbie 3 05-28-2009 08:40 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration