Unix aliases command not working properly

I have a command to kill some processes, as shown below:

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

the team works great

>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 shown 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 someone tell me what could be the problem?

+3
source share
4 answers

The command line in which you configure the alias is not quoted correctly. In particular, the built-in inverse quote subcommand is executed when the nickname is configured, and not later when you really want to run the nickname.

Try to configure it like this:

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

edit: awk - , .

+5

, xargs :

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

()

: , bash . :

killaf() { ps -fu $USER | awk '/[M]F1pp/ {print $2}' | xargs kill -9; }
+1

why do you want to use an alias? use a subroutine instead. And I suppose you mean the team ps, because I don’t know what psuis

killmyprocess(){
  ps -eo pid,comm |awk '$2~/MF1pp/{
    cmd="kill -9 "$1
    print cmd
  #  system(cmd) #uncomment to use
  }'
}
0
source

Try to elude $ in awk, usually it needs to be escaped for it to work fine:

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

Source: https://habr.com/ru/post/1736099/


All Articles