Passing shell variables to nawk snipet 
Author Message
 Passing shell variables to nawk snipet

I am try to isolate the pid of a running task (e.g. btfoocci).  I pass
two args to a korn shell script: directory + process name (e.g.
/src/bin btfoocci).  How can I get the pid of this task.    In my
shell $1 is /src/bin; $2 is btfoocci.  I get stuck at the following:

ps -e -o pid,args | nawk '($3=="$2") {print $1}'  The snipet does not
handle the "$2" correctly.  I have tried various combos "\$2", "'$2'".
 Than in Advance.



Thu, 25 Aug 2005 05:48:33 GMT  
 Passing shell variables to nawk snipet


X I am try to isolate the pid of a running task (e.g. btfoocci).  I pass
X two args to a korn shell script: directory + process name (e.g.
X /src/bin btfoocci).  How can I get the pid of this task.    In my
X shell $1 is /src/bin; $2 is btfoocci.  I get stuck at the following:
X
X ps -e -o pid,args | nawk '($3=="$2") {print $1}'  The snipet does not
X handle the "$2" correctly.  I have tried various combos "\$2", "'$2'".
X  Than in Advance.

Use the awk -v awk_variable_name="$2" as in:

    ps -e -o pid,args | nawk -v process_name="$2" '
        $3 == process_name {print $1}
    '

This would be perferred.  but you could also pass information via
environment variables:

    export PROCESS_NAME="$2"
    ps -e -o pid,args | nawk '
        $3 == ENVIRON["PROCESS_NAME"] {print $1}
    '

If you really want to play with shell variables inside of awk, then you
do strange stuff like:

    ps -e -o pid,args | nawk "\$3 == \"$2\" {print \$1}"

or this is another way to use shell variables inside of awk:

    ps -e -o pid,args | nawk '$3 == "'$2'" {print $1}'

Although I prefer the -v option as it is much cleaner way to pass
arguments into awk.

Good luck.

                                        Bob Harris



Thu, 25 Aug 2005 10:36:39 GMT  
 Passing shell variables to nawk snipet

Quote:



> X I am try to isolate the pid of a running task (e.g. btfoocci).  I pass
> X two args to a korn shell script: directory + process name (e.g.
> X /src/bin btfoocci).  How can I get the pid of this task.    In my
> X shell $1 is /src/bin; $2 is btfoocci.  I get stuck at the following:
> X
> X ps -e -o pid,args | nawk '($3=="$2") {print $1}'  The snipet does not
> X handle the "$2" correctly.  I have tried various combos "\$2", "'$2'".
> X  Than in Advance.

> Use the awk -v awk_variable_name="$2" as in:

>     ps -e -o pid,args | nawk -v process_name="$2" '
>         $3 == process_name {print $1}
>     '

The OP should also check whether the command "pgrep"
exists on his system, as this is what he is trying to implement.

John.



Thu, 25 Aug 2005 14:56:43 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. search on variable passed to awk /nawk

2. Passing result from nawk into several sh variables

3. Return value from awk snipet via korn shell subroutine

4. Passing command line variables to AWK in shell script

5. How to pass shell variable to awk?

6. pass a variable from awk to shell

7. passing shell variable into awk program

8. how do i pass shell variables into awk?

9. passing shell variable containing many fields to awk

10. Passing Shell Variables to Awk

11. Passing variable to shell in Awk...

12. passing a variable to a shell command

 

 
Powered by phpBB® Forum Software