From owner-freebsd-questions@FreeBSD.ORG Thu Jan 13 21:30:37 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AFB2B1065672 for ; Thu, 13 Jan 2011 21:30:37 +0000 (UTC) (envelope-from dteske@vicor.com) Received: from postoffice.vicor.com (postoffice.vicor.com [69.26.56.53]) by mx1.freebsd.org (Postfix) with ESMTP id 9AC1D8FC16 for ; Thu, 13 Jan 2011 21:30:37 +0000 (UTC) Received: from [208.206.78.30] (port=50274 helo=dt.vicor.com) by postoffice.vicor.com with esmtpsa (SSLv3:RC4-MD5:128) (Exim 4.71) (envelope-from ) id 1PdUkS-0007ZI-TX; Thu, 13 Jan 2011 13:30:32 -0800 From: Devin Teske To: Robert Bonomi In-Reply-To: <201101132045.p0DKj43V027817@mail.r-bonomi.com> References: <201101132045.p0DKj43V027817@mail.r-bonomi.com> Content-Type: text/plain Organization: Vicor, Inc Date: Thu, 13 Jan 2011 13:30:24 -0800 Message-Id: <1294954224.10908.9.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-41.el4) Content-Transfer-Encoding: 7bit X-Scan-Signature: 27c00b6c6b7cf5d60241f994e6a281a2 X-Scan-Host: postoffice.vicor.com Cc: f.bonnet@esiee.fr, freebsd-questions@freebsd.org Subject: Re: problem with shell script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Jan 2011 21:30:37 -0000 On Thu, 2011-01-13 at 14:45 -0600, Robert Bonomi wrote: > > Date: Wed, 12 Jan 2011 15:01:45 +0100 > > From: Frank Bonnet > > 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 <-