Shell variable within an awk script 
Author Message
 Shell variable within an awk script

Is it possible to assign a shell variable value within an awk script?

Thanks

--
Miguel Manso



Mon, 05 Nov 2001 03:00:00 GMT  
 Shell variable within an awk script

Quote:

>Is it possible to assign a shell variable value within an awk script?

Yes, use the system() function.  Read the man page for more
details.

man awk
man gawk

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 05 Nov 2001 03:00:00 GMT  
 Shell variable within an awk script


Quote:


> >Is it possible to assign a shell variable value within an awk script?

> Yes, use the system() function.  Read the man page for more
> details.

> man awk
> man gawk

Perhaps this is OS-dependent, but doesn't awk's system() function
invoke a subshell so that any environment variables set in the subshell
are lost when the subshell exits, in other words, when the system()
function completes and awk moves on to the next statement after the
system() call?

It may be possible for awk to write environment variable definitions in
the form 'variable=value' to a file or FIFO that the calling shell
reads from a line at a time and executes. Very messy and much more of a
shell programming exercise than an awk one.

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Mon, 05 Nov 2001 03:00:00 GMT  
 Shell variable within an awk script

% Is it possible to assign a shell variable value within an awk script?

In general, it's not possible to set a shell variable from anything but
a script running in that shell (that is, sourced into the shell).

However, Bourne shell derivatives allow you to take the standard output
of a process and treat it as if it had been typed on the command line,
so if all you want to do is set shell variables, you can do this:
 eval `awk 'BEGIN { print "a=" 2*3
              print "b=" 1231234/12342
 }'`
 echo $a $b
which prints
 6 99.7597

If you want the awk script to do some other stuff, this won't work
for you.

Note that the first quote (before "awk") is a left quote, the second
is a right quote, the second-last is a right quote, and the last one
is a left quote. With newer, wussy, follow-ons such as ksh and the
POSIX shell, you can use $(...) in place of `...`

--

Patrick TJ McPhee
East York  Canada



Tue, 06 Nov 2001 03:00:00 GMT  
 Shell variable within an awk script


   >Is it possible to assign a shell variable value within an awk
   >script?
In this example the variable isn't exactly set within the awk script, but:
VARIABLE=`awk '{print "x"}'`

Net-Tamer V 1.08X - Test Drive



Tue, 06 Nov 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. trying to call awk from within a shell script

2. shell commands within awk script

3. AWK-Script to set a shell variable (Win32) ?

4. Passing command line variables to AWK in shell script

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

6. shell variable in awk script

7. Access to Script Name Within Awk Script

8. awk variables --> shell variables

9. Set expect script variables via a korn shell script

10. Invoking shell command from within awk

11. executing a shell command within AWK

12. How do I display the contents of a shell variable from an awk script via a awk variable

 

 
Powered by phpBB® Forum Software