Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Fwd: passing variables from awk-parent to bash-child]

2 views
Skip to first unread message

V. Mark Lehky

unread,
Oct 5, 2006, 6:52:30 AM10/5/06
to bug-...@gnu.org
Hello,

I suspect I might have found an "issue", unfortunately I do not know if it is in
the program or documentation. :)
Below is what I have posted to Usenet. I also tried to modify the below program
to "print MY_VAR=different_value2; my_echo" into a pipe "/bin/sh", as is
suggested in the manual section which describes the command 'system'.
Nobody replied to this on Usenet, so I am turning to you in hopes of getting
somewhere.

Thank You, VML.


-------- Original Message --------
Subject: passing variables from awk-parent to bash-child
Date: Thu, 28 Sep 2006 10:16:20 +0100
From: SiKing <nos...@noway.invalid>
Organisation: A alphabetically be in organised sentence should words.
Newsgroups: comp.lang.awk,comp.unix.shell

Hello,

I am using gnu-awk 3.1.5 and bash 3.1.14 for the following.

I am trying to modify a (bash) environment variable (MY_VAR below) from an awk
script, and have that value propagate to a bash script child. I have managed to
distill my example down to the following:

main script:
#!/usr/bin/bash
export MY_VAR=value1
echo MY_VAR initial value is $MY_VAR
awk 'BEGIN {
print "MY_VAR value is still " ENVIRON["MY_VAR"]
ENVIRON["MY_VAR"] = "different_value2"
print "MY_VAR different value is " ENVIRON["MY_VAR"]
system( "my_echo" )
}' $0

my_echo script:
#!/usr/bin/bash
echo MY_VAR final value is $MY_VAR

The output is:
MY_VAR initial value is value1
MY_VAR value is still value1
MY_VAR different value is different_value2
MY_VAR final value is value1

What I was expecting was:
MY_VAR initial value is value1
MY_VAR value is still value1
MY_VAR different value is different_value2
MY_VAR final value is different_value2

Where have I gone wrong?
My current solution is to pass the value through a file. But I don't really like
that solution. Is there a better way of passing information in this direction?

TIA, SK.

--
"One of these days, milkshake, BOOM!"
-- Evil Midnight Bomber What Bombs at Midnight
-----
Candy for spammers:
http://members.shaw.ca/grubb/spamthis.html

--
if my calculations are correct,
SLINKY + ESCALATOR = EVERLASTING FUN
<http://ca.blog.360.yahoo.com/egeer987>


Andrew J. Schorr

unread,
Oct 5, 2006, 4:47:17 PM10/5/06
to V. Mark Lehky, bug-...@gnu.org
On Thu, Oct 05, 2006 at 11:52:30AM +0100, V. Mark Lehky wrote:
> I suspect I might have found an "issue", unfortunately I do not know if it
> is in the program or documentation. :)
> Below is what I have posted to Usenet. I also tried to modify the below
> program to "print MY_VAR=different_value2; my_echo" into a pipe "/bin/sh",
> as is suggested in the manual section which describes the command 'system'.
> Nobody replied to this on Usenet, so I am turning to you in hopes of
> getting somewhere.

As currently implemented, the gawk ENVIRON array does not support
your desired behavior. To quote from gawk.info:

`ENVIRON'
An associative array that contains the values of the environment.
The array indices are the environment variable names; the elements
are the values of the particular environment variables. For
example, `ENVIRON["HOME"]' might be `/home/arnold'. Changing this
array does not affect the environment passed on to any programs
that `awk' may spawn via redirection or the `system' function.

Note the last sentence. So gawk is working as documented. To achieve your
desired behavior would require patching gawk to use putenv or setenv to
manipulate the environment whenever ENVIRON is changed (or just before forking
a child process). If you develop such a patch, I imagine there may be some
interest in it...

Regards,
Andy


Aharon Robbins

unread,
Oct 5, 2006, 6:17:12 PM10/5/06
to bug-...@gnu.org, sik...@myrealbox.com
Greetings. Re this:

> Date: Thu, 05 Oct 2006 11:52:30 +0100
> From: "V. Mark Lehky" <sik...@myrealbox.com>
> Subject: [Fwd: passing variables from awk-parent to bash-child]
> To: bug-...@gnu.org
>
> Hello,


>
> I suspect I might have found an "issue", unfortunately I do not know if it is in
> the program or documentation. :)
> Below is what I have posted to Usenet. I also tried to modify the below program
> to "print MY_VAR=different_value2; my_echo" into a pipe "/bin/sh", as is
> suggested in the manual section which describes the command 'system'.
> Nobody replied to this on Usenet, so I am turning to you in hopes of getting
> somewhere.
>

> Thank You, VML.
>
>
> -------- Original Message --------
> Subject: passing variables from awk-parent to bash-child
> Date: Thu, 28 Sep 2006 10:16:20 +0100
> From: SiKing <nos...@noway.invalid>
> Organisation: A alphabetically be in organised sentence should words.
> Newsgroups: comp.lang.awk,comp.unix.shell
>
> Hello,
>
> I am using gnu-awk 3.1.5 and bash 3.1.14 for the following.
>
> I am trying to modify a (bash) environment variable (MY_VAR below) from an awk
> script, and have that value propagate to a bash script child. I have managed to
> distill my example down to the following:
>
> main script:
> #!/usr/bin/bash
> export MY_VAR=value1
> echo MY_VAR initial value is $MY_VAR
> awk 'BEGIN {
> print "MY_VAR value is still " ENVIRON["MY_VAR"]
> ENVIRON["MY_VAR"] = "different_value2"
> print "MY_VAR different value is " ENVIRON["MY_VAR"]
> system( "my_echo" )
> }' $0

As Andy pointed out in a separate mail, modifying ENVIRON doesn't
affect the environment seen by child processes. If you change
the call to system() to something like:

system ("MY_VAR=" ENVIRON["MY_VAR"] "; export MY_VAR; my_echo" )

it ought to work.

HTH,

Arnold


0 new messages