View Single Post

   
  #5 (permalink)  
Old 02-15-2008, 04:07 PM
Ronald J Marchand
 
Posts: n/a
Default Re: Using an awk program

"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.

Thanks
Ron


Reply With Quote