From owner-freebsd-questions Sun Mar 17 23:52:29 2002 Delivered-To: freebsd-questions@freebsd.org Received: from hawk.mail.pas.earthlink.net (hawk.mail.pas.earthlink.net [207.217.120.22]) by hub.freebsd.org (Postfix) with ESMTP id DD4AF37B404 for ; Sun, 17 Mar 2002 23:52:24 -0800 (PST) Received: from sdn-ar-007dcwashp005.dialsprint.net ([63.178.91.13] helo=moo.holy.cow) by hawk.mail.pas.earthlink.net with esmtp (Exim 3.33 #1) id 16mrwN-0002r9-00; Sun, 17 Mar 2002 23:52:24 -0800 Received: by moo.holy.cow (Postfix, from userid 1001) id 9FD1550B8B; Mon, 18 Mar 2002 02:54:40 -0500 (EST) Date: Mon, 18 Mar 2002 02:54:40 -0500 From: parv To: D J Hawkey Jr Cc: freebsd-questions@freebsd.org Subject: Re: awk - any possibility of using a variable in regex Message-ID: <20020318075440.GA3810@moo.holy.cow> Mail-Followup-To: D J Hawkey Jr , freebsd-questions@freebsd.org References: <20020317205228.A22100@sheol.localdomain> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20020317205228.A22100@sheol.localdomain> Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG in message <20020317205228.A22100@sheol.localdomain>, wrote D J Hawkey Jr thusly... > > My bad to reply to myself, but I remember I *do* have something like this, > without the need for perl or awk: ... > #!/bin/sh > > for i in ico mouseclock pyro xcursor xearth xfishtank xneko xroach; do > killall -9 -u $LOGNAME -c $i 2>/dev/null > done that doesn't work here if a program is started w/ full path, say, "/usr/X11R6/bin/xeyes" instead of "xeyes". from killall man page, it seems that it should work, kind of ps -c output, but it doesn't. perhaps a bug? > #!/bin/sh > > TMP=/var/tmp/$0.$$ > > teststop () > { > [ `ps -aO ruser |egrep $LOGNAME.+$1 | grep -v grep | tee $TMP | wc -l` -eq 1 ] && kill -9 `cat $TMP | awk '{ print $1 }'` > rm -f $TMP > } i was so intent on using awk to avoid two greps like above that i missed the fact that i could have used variables w/ grep. thanks much dave for the reminder. below is the resulting script. #!/bin/sh user=${USER:-${LOGNAME}} for proc in startx xinit # etc... do pids=$(ps -a -O ruser -c | egrep "${user}.+\<${proc}\>" | awk '!/grep/{ print $1 }') #echo pids: $pids [ x"${pids}" = x ] && continue count=$(echo $pids | wc -l) #echo count: $count [ $count -eq 0 ] && continue for pid in $pids do #echo killing pid: $pid /bin/kill -SIGKILL $pid done done # end - parv -- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message