views:

195

answers:

4

i have a command to kill some processes as below:

kill -9 `psu|grep MF1pp|grep -v grep|awk '{print $2}'`

the command works perfectly fine

>psu|grep MF1pp|grep -v grep|awk '{print $2}'
29390
29026
$>kill -9 `psu|grep MF1pp|grep -v grep|awk '{print $2}'`
$>psu|grep MF1pp|grep -v grep|awk '{print $2}'

when i create an alias as below and run it:

alias killaf="kill -9 `psu|grep MF1pp|grep -v grep|awk '{print $2}'`"



$> psu|grep MF1pp|grep -v grep|awk '{print $2}'
5487
5272
$>killaf
ksh: kill: bad argument count

gives the above error.

can anyone tell me what could be the issue?

A: 

Try escaping the $ in awk, usually it must be escaped to make it work fine:

alias killaf="kill -9 `psu|grep MF1pp|grep -v grep|awk '{print \$2}'`"
Lex
no, that won't help I don't think.
Pointy
this does not work:(
Vijay Sarathi
+3  A: 

The command line whereby you're setting up the alias is not quoted correctly. Specifically, the back-quote embedded subcommand is being executed at the time you set up the alias, and not later when you actually want to run the alias.

Try setting it up this way:

alias killaf='kill -9 `psu|grep MF1pp|grep -v grep|awk '\''{print $2}'\''`'

edit: I fixed the quotes around the awk command - it's tricky to embed single-quotes when you're already single-quoting.

Pointy
good answer.this works perfectly:)
Vijay Sarathi
why exactly are the `'\'` 's required?
Vijay Sarathi
A: 

why do you want to use an alias? use a sub routine instead. And i assume you mean ps command as i don't know what psu is

killmyprocess(){
  ps -eo pid,comm |awk '$2~/MF1pp/{
    cmd="kill -9 "$1
    print cmd
  #  system(cmd) #uncomment to use
  }'
}
ghostdog74
psu here is `ps-fu $USER`.its again an alias.
Vijay Sarathi
+1  A: 

This is what xargs is for:

ps -fu $USER | awk '/[M]F1pp/ {print $2}' | xargs kill -9

(untested)

reinierpost