Return value from awk snipet via korn shell subroutine 
Author Message
 Return value from awk snipet via korn shell subroutine

I would like to return a value from the following AWK snipet:

function getpid {
ps -e -o pid,args | nawk -v path="$1" -v pname="$2 '($3 == pname) &&
(substr($2,1,length($1)-0)) {print $1}'

Quote:
}

The pid is printed out(this is correct);  But as opposed to printing I
would like the routine to return the value.  Many Thanx in Advance.


Sat, 27 Aug 2005 02:17:21 GMT  
 Return value from awk snipet via korn shell subroutine

Quote:

>I would like to return a value from the following AWK snipet:

>function getpid {
>ps -e -o pid,args | nawk -v path="$1" -v pname="$2 '($3 == pname) &&
>(substr($2,1,length($1)-0)) {print $1}'
>}
>The pid is printed out(this is correct);  But as opposed to printing I
>would like the routine to return the value.  Many Thanx in Advance.

Note to CDemas: This *would* be an appropriate time for me to point out
that if I fed the above to an AWK interpreter, all I would get is error
messages.

I think the source of the confusion is that both AWK and (extremely
current versions of some) shells have "function" as a keyword and have the
above syntax in place for declaring functions.

Anyway, to the OP, you need to learn the difference between shell and AWK,
and then, once that is firmly understood, lookup the details on the AWK
"getline" function.

HTH



Sat, 27 Aug 2005 02:38:21 GMT  
 Return value from awk snipet via korn shell subroutine

Quote:



> >I would like to return a value from the following AWK snipet:

> >function getpid {
> >ps -e -o pid,args | nawk -v path="$1" -v pname="$2 '($3 == pname) &&
> >(substr($2,1,length($1)-0)) {print $1}'
> >}
> >The pid is printed out(this is correct);  But as opposed to printing I
> >would like the routine to return the value.  Many Thanx in Advance.

> Note to CDemas: This *would* be an appropriate time for me to point out
> that if I fed the above to an AWK interpreter, all I would get is error
> messages.

> I think the source of the confusion is that both AWK and (extremely
> current versions of some) shells have "function" as a keyword and have the
> above syntax in place for declaring functions.

> Anyway, to the OP, you need to learn the difference between shell and AWK,
> and then, once that is firmly understood, lookup the details on the AWK
> "getline" function.

> HTH

Thanx anyway.


Sat, 27 Aug 2005 06:24:06 GMT  
 Return value from awk snipet via korn shell subroutine


X I would like to return a value from the following AWK snipet:
X
X function getpid {
X ps -e -o pid,args | nawk -v path="$1" -v pname="$2 '($3 == pname) &&
X (substr($2,1,length($1)-0)) {print $1}'
X }
X The pid is printed out(this is correct);  But as opposed to printing I
X would like the routine to return the value.  Many Thanx in Advance.

Since getpid is a ksh function, try this when invoking getpid

        shell_variable=$(getpid path pname)

where path and pname are the values you wish to specify for $1 and $2
inside of getpid.

If you want getpid to set some shell variable before returning, then try
something like this.

function getpid {
    shell_variable=$(ps -e -o pid,args | \
                     nawk -v path="$1" -v pname="$2" '
                         ($3 == pname) && (substr($2,1,length($1)-0)) {
                             print $1
                         }
                     ')

Quote:
}

NOTE:  In your example, you are missing a " after $2

                                        Bob Harris



Sat, 27 Aug 2005 07:04:48 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Set expect script variables via a korn shell script

2. passing parameter from awk back to korn shell

3. Passing shell variables to nawk snipet

4. Substituting single quotes from Perl via shell using awk and gsub

5. Write statement in subroutine affecting output of return values

6. help for newbie: way to get the value of an awk variable in a shell script

7. returning values from a function in a shell script

8. Getting awk computed value into shell variable

9. giving awk values back to the shell

10. How to return value to shell variable

11. Quick Basic - How to get a returned value from the DOS shell

12. Return value to shell?

 

 
Powered by phpBB® Forum Software