38.13 Interactively Kill Processes Matching a PatternWhen you want to kill processes, it's a pain in the neck to run ps ( 38.5 ) , figure out the process ID, and then kill the process. The zap shell script was presented by Brian Kernighan and Rob Pike in their classic book The UNIX Programming Environment . The script uses egrep ( 27.5 ) to pick the processes to kill; you can type extended expressions that match more than one process, such as:
% We've reprinted the script by permission of the authors:
The This shell version of zap calls another script, pick , shown below. [6] pick shows each of its command-line arguments and waits for you to type y , q , or anything else. Answering y writes the line to standard output, answering q aborts pick without showing more lines, and any other answer shows the next input line without printing the current one. zap uses awk ( 33.11 ) to print the first argument (the process ID number) from any ps line you've selected with pick . The inner set of nested ( 45.31 ) backquotes ( 9.16 ) in zap pass pick the output of ps , filtered through egrep . Because the zap script has set the IFS variable ( 35.21 ) to just a newline, pick gets and displays each line of ps output as a single argument. The outer set of backquotes passes kill ( 38.10 ) the output of pick , filtered through awk .
If you're interested in shell programming and that explanation wasn't detailed enough, take a careful look at the scripts - they're really worth studying. (This book's shell programming chapters, 44 through 46, may help, too.) Here's the pick script:
- |
|