passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working):

if [[ -n $1 ]];then
searchstr=$1
lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr'
else
echo "usage: $0 <search string>"
fi

I tried several options. Is it posisble to do this without writing a sepoerate awk script and load it with: awk -f script.awk var=$value

I prefer to have all the code in the same script.

[The script tries to get all pid's assosciated with a certain portnumber.]

lsof -i | awk -v srch=$searchstr '{if(index($9,srch)>0) print $2} '

I wasn't positive what you need - I just hacked together something.
Short answer: Use -v

I was looking for someting like this:

if [[ -n $1 ]];then
lsof -i | awk '{if($9~/'$1'/) print $2}'
else
echo "usage: $0 <search string>"
fi

Just unqoute $1 in awk and it gets expanded by the shell and it also works.

Thanbks for the help.

rein, just remove the \ around SEARCHSTR and move the last ' :

if [[ -n $1 ]];then
searchstr=$1
lsof -i | awk '{if($9~SEARCHSTR) print $2}' SEARCHSTR=$searchstr
else
echo "usage: $0 <search string>"
fi

The variable SEARCHSTR is avalaible in all your awk code except in the BEGIN pattern.

If you want to use the variable in BEGIN, use the following syntax (with a little variation in the awk code) :

if [[ -n $1 ]];then
lsof -i | awk -v SEARCHSTR=$1 '$9~SEARCHSTR {print $2}' 
else
echo "usage: $0 <search string>"
fi

Jean-Pierre.