I have these lines in one shell script file foo.sh:
ps ax | grep -E "bar" | grep -v "grep" | awk '{print $1}' | xargs kill -9 $1
when I execute the shell script with an arguments like this:
sh foo.sh arg_one
the xargs can’t work now. It takes the $1 from the shell script but not the output of awk.
I do know I can store the output of awk into one file and use it in xargs later.
But, is there any better solution?
== edited ==
thanks the answer from @peterph.
But, is there any way that I can use $1 in xargs?
== edited 2 ==
thanks @Brian Campbell
Despite weather there should be a useless $1 in the example, if a argument of “the shell script file” is given, then the $1 in xargs will not work as my wish, in my computer(In your computer too, I think).
Why? And, how to get avoid it?
xargsreads list fromstdinso just discard the last$1on the line if what you want is to kill processes by their PIDs.As a side note,
pscan also print processes according to their command name (with procps on linux see the-Coption).