Re: Using an awk program "Bob Stockler" <bob@trebor.iglou.com> wrote in message
news:20040909155625.GA2244@trebor.iglou.com...
> On Thu, Sep 09, 2004 at 07:54:42AM -0500, Ronald J Marchand wrote:
> | "Stuart J. Browne" <stuart@promed.com.au> wrote in message
> | news:413f9da9@dnews.tpgi.com.au...
> | > > Using an awk program, I am trying to set the values of pid and
pseudo in
> | > > a
> | > > shell script to be used in other commands. So far I have:
> | > > :
> | > > logname=digna
> | > > pid=
> | > > pseudo=
> | > > ps -ef | grep xcrt | awk -v user=$logname -v pid=$pid -v
pseudo=$pseudo
> | '
> | > > ( $1 == user && $8 == "/usr/lib/merge/xcrt" ) { print $1,$2 }
> | > > ( $1 == user && $8 == "/usr/lib/merge/xcrt" ) { pid = $2 }
> | > > ( $1 == user && $8 == "/usr/lib/merge/xcrt" ) { pseudo = $6 }
> | > > '
> | > > echo $pid
> | > > echo $pseudo
> | > > exit
> | > >
> | > > This program executes fine. It just doesn't set pid & pseudo.
> | > > Could some kind soul show mercy and explain the error?????
> | >
> | > So you want the environment variables 'pid' and 'pseudo' after ther
fact?
> | > you can't do that.
> | >
> | > You either use ``'s to fill them, or you do everything from within
awk.
> | >
> | > Example:
> | >
> | > #!/usr/bin/awk -f
> | > BEGIN {
> | > cmd = "/bin/ps -fU " ENVIRON["logname"]
> | > while ( cmd | getline ) {
> | > if ( $8 ~ /xcrt$/ ) {
> | > pid = $2
> | > pseudo = $6
> | > system( "<do command here>" )
> |
> | This I will explore. thanks
> |
> | > }
> | > }
> | > close( cmd )
> | > }
> | >
> | > If you just want to populate environment variables in a shell for
> | > lots-of-other-purposes, then you'd have to use it completely
differently:
> | >
> | > :
> | > pid=`ps -fU $logname | awk '$8 ~ /xcrt$/ { print $2 }'`
> | > pseudo=`ps -fU $logname | awk '$8 ~ /xcrt$/ { print $6 }'`
> | >
> | > However, if there are multiple PID's for this one, it will only grab
the
> | > 'last' PID/TTY pair.
> | >
> | I am aware of how to use accent grave to set a variable in the caller.
While
> | this little project has a purpose, it is also a learning project on the
use
> | of awk. I just hoped that if you could pass one piece of information
back
> | to the caller, there could be a way to pass multiple variables.
Apparently
> | not. It seems inefficient to run the same (similar) script several
times.
>
> Sure there is, by using "eval" and command substitution:
>
> eval `
> awk '{
> # your awk program to determine the values
> # you want to assign to var1, var2, var3 ...
> }
> END {
> print "var1=" var1
> print "var2=" var2
> print "var3=" var3
> }' filename # or piped in standard input
> `
>
> Bob
>
Thanks. JP indicated this but I didn't understand what he meant
Ron |