Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 13 Jan 2011 13:30:24 -0800
From:      Devin Teske <dteske@vicor.com>
To:        Robert Bonomi <bonomi@mail.r-bonomi.com>
Cc:        f.bonnet@esiee.fr, freebsd-questions@freebsd.org
Subject:   Re: problem with shell script
Message-ID:  <1294954224.10908.9.camel@localhost.localdomain>
In-Reply-To: <201101132045.p0DKj43V027817@mail.r-bonomi.com>
References:  <201101132045.p0DKj43V027817@mail.r-bonomi.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 2011-01-13 at 14:45 -0600, Robert Bonomi wrote:
> > Date: Wed, 12 Jan 2011 15:01:45 +0100
> > From: Frank Bonnet <f.bonnet@esiee.fr>
> > Subject: problem with shell script
> >
> > Hello
> >
> > I'm in trouble with a simple shell script that give erroneous value when 
> > running ...
> >
> > If I run commands interactively everything runs well
> >
> >  > ps ax | grep slapd | grep -v grep | wc -l
> >        1
> >
> > If I run in the following shell script :
> >
> > #!/bin/sh
> > SD=0 SD=`ps -ax | grep slapd | grep -v grep | wc -l` echo $SD
> >
> > the result is 3 !!!
> 
> Advice: don't try to 'out-think' the machine -- make it _show_ you what it
>  is doing.

Might I suggest the oft-overlooked "o" option to ps(1)...

	ps axo ucomm | grep -c slapd

I can't think of anything better to achieve what the OP wanted.

Naturally... for those that need more (that is, if you want to see
meaningful output rather than just the line-count)...

try:

	ps axo pid,state,ucomm

or:

	ps axo pid,command | awk '$2~/slapd/{print}'

naturally, for ``wider'' output:

	ps axwwwo pid,command | awk '$2~/slapd/{print}'

And the creme-de-la-creme:

	ps axwwwo pid,command | awk '$2~"^slapd$"||$2~"/slapd$"{print}'

which will only print a line if the first word of the entire command is
wholly "slapd" or ends in "/slapd".

Additional solutions/combinations left as an exercise to the reader.
--
Devin

(full sig at bottom)


> 
> Change the script to:
>    SD=`ps -ax | grep slapd | grep -v grep | tee /dev/tty | wc -l` echo $SD
> 
> 
> I suspect thet the -name- of the script file has 'slapd' in it.
> 
> 
> 
> _______________________________________________
> freebsd-questions@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe@freebsd.org"
-- 
Cheers,
Devin Teske

-> CONTACT INFORMATION <-
Business Solutions Consultant II
FIS - fisglobal.com
510-735-5650 Mobile
510-621-2038 Office
510-621-2020 Office Fax
909-477-4578 Home/Fax
devin.teske@fisglobal.com

-> LEGAL DISCLAIMER <-
This message  contains confidential  and proprietary  information
of the sender,  and is intended only for the person(s) to whom it
is addressed. Any use, distribution, copying or disclosure by any
other person  is strictly prohibited.  If you have  received this
message in error,  please notify  the e-mail sender  immediately,
and delete the original message without making a copy.

-> FUN STUFF <-
-----BEGIN GEEK CODE BLOCK-----
Version 3.1
GAT/CS d(+) s: a- C++(++++) UB++++$ P++(++++) L++(++++) !E--- W++ N? o? K- w O
M+ V- PS+ PE Y+ PGP- t(+) 5? X+(++) R>++ tv(+) b+(++) DI+(++) D(+) G+>++ e>+ h
r>++ y+ 
------END GEEK CODE BLOCK------
http://www.geekcode.com/

-> END TRANSMISSION <-




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1294954224.10908.9.camel>