drears@pilot.njin.net (Dennis G. Rears) (03/19/91)
I need to write a shell script that will allow users to kill all thier processes associated that a program name. This script will run on a HP 9000/835, which is a SYSV Unix. I am having problems with the awk script in the following script fragment: CMD='artemis' ME=`whoami` PROC=`ps -ef|grep /${CMD}| \ awk '{ $1 == ${ME} && $8 != "grep" $2 }' ` kill -HUP ${PROC} I want awk to output the process numbers (Field 2) where the username (field 1) is equal to the variable ${ME} and field 8 is not equal to 'grep'. Once I get that I can just do a "kill -HUP ${PROC}" Can anyone tell me what I am doing wrong or suggest a different way of doing it. This is one of my first attempts at using awk. Thanks in advance. Dennis
tchrist@convex.COM (Tom Christiansen) (03/19/91)
From the keyboard of drears@pica.army.mil: : I need to write a shell script that will allow users to kill all thier :processes associated that a program name. This script will run on a HP :9000/835, which is a SYSV Unix. : : I am having problems with the awk script in the following script :fragment: : :CMD='artemis' :ME=`whoami` :PROC=`ps -ef|grep /${CMD}| \ :awk '{ $1 == ${ME} && $8 != "grep" $2 }' ` :kill -HUP ${PROC} Two problems: you don't want that slash in the grep command, you can't depend on field counts on ps (on mine at least) There is a nice zap script in the perl book, or available on uunet. --tom
jik@athena.mit.edu (Jonathan I. Kamens) (03/19/91)
In article <Mar.18.11.52.08.1991.16656@pilot.njin.net>, drears@pilot.njin.net (Dennis G. Rears) writes: |> CMD='artemis' |> ME=`whoami` |> PROC=`ps -ef|grep /${CMD}| \ This seems like a problem to me. Programs in a ps listing don't always have the full path of the executable listed. With the slash in the command above, you're requiring the full path listing, which is probably going to lose in some cases. |> awk '{ $1 == ${ME} && $8 != "grep" $2 }' ` First of all, the conditions should be outside the curly braces. Second, you can't just give a field and expect awk to print it -- you have to tell awk to print it. Third, few versions of awk allow you to access environment variables inside awk, and that's what you're trying to do above, since the ${ME} inside the single quotes is going to be interpreted by awk, not by the shell. Try this: awk '$1 == '${ME}' && $8 != "grep" {print $2}'` -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710
sadkins@oucsace.cs.OHIOU.EDU (Scott W. Adkins) (03/19/91)
In article <1991Mar18.182106.2313@convex.com> tchrist@convex.COM (Tom Christiansen) writes: >: >:CMD='artemis' >:ME=`whoami` >:PROC=`ps -ef|grep /${CMD}| \ >:awk '{ $1 == ${ME} && $8 != "grep" $2 }' ` >:kill -HUP ${PROC} > >Two problems: > you don't want that slash in the grep command, > you can't depend on field counts on ps (on mine at least) If you are trying to parse the command from the 'ps' line, I merrily use current=substr($0,50) to get it all into a variable. The reason you can't depend on field counts is because some commands have spaces in them, such as 'vi file'. Others may be shell scripts or sub-processes that may start with '/bin/sh blah blah' or even 'sh -c blah blah'. It is really hard to count on field counts with 100% success. Scott Adkins sadkins@oucsace.cs.ohiou.edu (my .signature file has the flu...)
lewis@tramp.Colorado.EDU (LEWIS WILLIAM M JR) (03/20/91)
To: jik@athena.mit.edu Subject: Re: awk script Newsgroups: comp.unix.questions In-Reply-To: <1991Mar18.193118.17247@athena.mit.edu> References: <Mar.18.11.52.08.1991.16656@pilot.njin.net> Organization: University of Colorado, Boulder Cc: Bcc: In article <1991Mar18.193118.17247@athena.mit.edu> you write: ... stuff deleted ... >you can't just give a field and expect awk to print it -- you have to tell awk >to print it. WRONG! WRONG! WRONG! See page 10 of the nawk book or Section 2.2 (page 3) of the original Aho, Kernighan and Weinberger paper. The default action is to print $0.
snpatel@ncsuvx.enet.dec.com (SHETAL NATAWARBH PATEL) (03/20/91)
> > CMD='dxclock' > ME=`whoami` > PROC=`ps -ef | grep /${CMD}| > awk '$1 == '${ME}' && $8 != "grep" {print $2}'` > kill -HUP ${PROC} > I tried this script and it doesn't work. But I finally got this to work, CMD='dxclock' ME=`whoami` PROC=`ps -aux | grep ${CMD}|grep -v grep| \ awk '{print $2}'` kill -9 ${PROC} Is there any way to ask the user to type in the command that they wish to kill and then kill it after the entry? -- Nothing more smooth than glass, yet nothing more brittle; Nothing more fine than wit, yet nothing more fickle. -=> Thomas Fuller, Gnomologia No. 6472 <=- +-----------------------------------------------------------------+ | Shetal N. Patel | | snpatel@eos.ncsu.edu NORTH CAROLINA STATE UNIVERSITY | +-----------------------------------------------------------------+
jik@athena.mit.edu (Jonathan I. Kamens) (03/20/91)
In article <1991Mar19.171332.28055@colorado.edu>, lewis@tramp.Colorado.EDU (LEWIS WILLIAM M JR) writes: |> WRONG! WRONG! WRONG! See page 10 of the nawk book or Section 2.2 (page 3) of |> the original Aho, Kernighan and Weinberger paper. The default action is |> to print $0. I am quite aware of the default action. But that wasn't what I was referring to. You should read a little bit more carefully before you tell someone that he is "WRONG! WRONG! WRONG!" The poster to which I was responding had this awk code: awk '{ $1 == ${ME} && $8 != "grep" $2 }' ` Now, as I said in my last posting in this thread, that is not valid because he has simply specified a field and expected awk to print it. This is different from the "default action" of printing $0, because the default action only takes place when there is no '{ ... }' for awk to evaluate. Don't believe me? Well, this (trivial example) doesn't work for me: ls -l | awk '{$2}' and this does: ls -l | awk '{print $2}' -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710