0

In a Bash script:

#!/usr/bin/bash

#grep()
#{
#grep -q
#}

if [[ -z $1 ]];
then
  echo "usage: pkill -signal process name "
fi

if [[ -z $2 ]];
then
  echo "error: not enough parameters "
  exit
fi    

kill9="9"
kill15="15"

if [ $1 -eq $kill9 ]
then
  set "9" 
else
  set "15"
fi

ARRAY=(
  `ps -ef|grep $2 |grep -v grep|awk '{print \$2}'`
)

pkill()
{
  PIDTODIE=${2}
  for i in ${ARRAY[@]} ;do kill $1  $i;done
}

pkill $1 $2 

Is it possible to assign in awk, $2 so that it becomes $b? Because $2 is a Bash parameter and gives me conflicts. Also the \$2 is not working.

slm
  • 369,824
elbarna
  • 12,695

1 Answers1

1

You can set variables in awk from the shell like so:

$ somevar=4
$ echo | awk -v my_var="$somevar" '{print "My var is " my_var}'
My var is 4

References

slm
  • 369,824
  • which of those categories do you fit in? ;-) (notice how your code nicely matches the code injection vulnerability example I give in my answer there). – Stéphane Chazelas Dec 05 '14 at 12:00
  • @StéphaneChazelas - thank you, I hadn't had the time to read through your Q&A yet (that's on my weekend reading list, though I'll bump it up to the top now 8-)). I'd never seen those variables quoted when being passed into awk before and hadn't realized that they too required quoting there. That code snippet was a copy/paste from a SO Q&A that I referenced, BTW. I'll edit that one as well. I'm that proficient w/ AWK. – slm Dec 05 '14 at 12:36
  • @StéphaneChazelas - ah the example had my_var=4 and an example further down that included my_var="$some_other", so it was all my bad. – slm Dec 05 '14 at 12:40