vBulletin Search Engine Optimization
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| 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????? TIA Ron |
| |||
| > 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>" ) } } 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. Hope this helps. |
| |||
| "Ronald J Marchand" <ron@rojomar.com> wrote in message news:837cf$413f9652$42a6716f$20133@msgid.meganewss ervers.com... | 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: Look at the shell manual for the purpose of a single period on a command line followed by a space followed by a command. Look also at the purpose of the back quotes. NM |
| |||
| Ronald J Marchand typed (on Wed, Sep 08, 2004 at 06:32:24PM -0500): | 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????? A parent does not inherit from its child. Whatever you set inside the awk program is unknown outside it. Your grep is superfluous, BTW. Try this: eval `ps -ef | awk -v user=$LOGNAME ' $1~user && $8~"/usr/lib/merge/xcrt" {print "pid=" $2 " pseudo=" $6} '` echo PID is $pid and PSEUDO-TTY is $pseudo -- JP |
| |||
| "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 |
| |||
| "Jean-Pierre Radley" <jpr@jpr.com> wrote in message news:20040909002807.GB22610@jpradley.jpr.com... > Ronald J Marchand typed (on Wed, Sep 08, 2004 at 06:32:24PM -0500): > | 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????? > > A parent does not inherit from its child. Whatever you set inside the > awk program is unknown outside it. > > Your grep is superfluous, BTW. You are in fact correct. I started building from what I used on the command line. > > Try this: > > eval `ps -ef | awk -v user=$LOGNAME ' > $1~user && $8~"/usr/lib/merge/xcrt" {print "pid=" $2 " pseudo=" $6} > '` > echo PID is $pid and PSEUDO-TTY is $pseudo In this case I am writing a script to do a job which requires 2 variables. I can execute the awk program twice with accent grave to set the variables. But what if you wanted 6? Apparently you can direct the output to a file ( I have never done it ... yet). I was hoping there was a way to redirect this back into the script without using a file. Thanks Ron |
| |||
| 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 -- Bob Stockler - bob@trebor.iglou.com Author: MENU EDIT II - The BEST Creator/Editor/Manager for filePro User Menus. Fully functional (time-limited) demos available by email request (specify OS). |
| ||||
| "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 |