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

windows scripting with gawk

172 views
Skip to first unread message

Serious_Developer

unread,
Sep 14, 2012, 1:51:05 PM9/14/12
to
I must recall that thread because I did not find other group who could
be asked. Sorry for that inconvenience but I have to ask this again
for the urgent need .
I am calling that batch file that I want to return to it a value from
gawk script but it is not effective ????
ENVIRON["codeinsidebat"]=codeawk

%binaries%\gawk -f %scripts%\cyprusbank_distribute_branches.awk %source
%\%%* 2> %scripts%\cyprusbank_distribute_branches.fil
echo from inside batch %codeinsidebat%

Ed Morton

unread,
Sep 14, 2012, 2:35:49 PM9/14/12
to
Serious_Developer <ehabaz...@gmail.com> wrote:

> I must recall that thread because I did not find other group who could
> be asked. Sorry for that inconvenience but I have to ask this again
> for the urgent need .
> I am calling that batch file that I want to return to it a value from
> gawk script but it is not effective ????
> ENVIRON["codeinsidebat"]=codeawk
<snip>

Ignoring the Windows/batch stuff it sounds like you're trying to change
the value of an environment variable by changing it's value in the
ENVIRON[] array within awk. You simply can't do that. ENVIRON contains a
COPY of the environment variables names and associated values when awk
starts up, that's all.

Look (in UNIX, no idea what the Windows equivalent is):

$ x=7; awk 'BEGIN{ ENVIRON["x"]=8; print ENVIRON["x"] }'; echo "$x"
8
7

If you want to change the value of a variable in your environment as a
result of executing an awk script then you need to have awk return that
value and populate your variable from it, e.g.

$ x=7; x=$(awk 'BEGIN{ ENVIRON["x"]=8; print ENVIRON["x"] }'); echo "$x"
8

or generate a script to set the value and execute that script, e.g. with
UNIX eval:

$ x=7; eval $(awk 'BEGIN{ ENVIRON["x"]=8; print "x="ENVIRON["x"] }'); echo
"$x"
8

Regards,

Ed.

Posted using www.webuse.net

pop

unread,
Sep 14, 2012, 6:53:54 PM9/14/12
to
Ed Morton said the following on 9/14/2012 1:35 PM:
There is no unix/linux equivalent method to set an environment variable
in windows as explained above. The only way I have been able to do it
(within [g]awk) is as follows:

set x=7
echo %x%
7
gawk "BEGIN{ x=8;print \"@set x=\"x >\"setx.bat\" }"
call setx
echo %x%
8

Note that "call" is used if in a batch file and further batch statements
are present since a batch file is immediately exited without the "call".

HTH
pop is Mark

Manuel Collado

unread,
Sep 15, 2012, 10:56:36 AM9/15/12
to
El 15/09/2012 0:53, pop escribiᅵ:
On Windows XP and later:

awk "BEGIN {exit 8}"
set x=%ERRORLEVEL%
echo %x%
8

--
Manuel Collado - http://lml.ls.fi.upm.es/~mcollado



Luuk

unread,
Sep 15, 2012, 11:19:20 AM9/15/12
to
Why on 'Windows or later'?
The same works on *nix ;)

#!/bin/bash
awk 'BEGIN{ exit 123}'
x=$?
echo $x

Kenny McCormack

unread,
Sep 15, 2012, 11:34:07 AM9/15/12
to
In article <ksiei9-...@luuk.invalid.lan>, Luuk <lu...@invalid.lan> wrote:
>On 15-09-2012 16:56, Manuel Collado wrote:
>> El 15/09/2012 0:53, pop escribi�:
It should be clear by now that "*nix" is completely irrelevant in the
context of this newsgroup thread.

--
Given Bush and his insanely expensive wars (*), that we will be paying for
for generations to come, the only possible response a sensible person need
ever give, when a GOPer/TeaBagger says anything about "deficits", is a
polite snicker.

(*) Obvious money transfers between the taxpayers and Bush's moneyed
interests. Someday, we'll actually figure out a way to have a war where the
money just gets moved around and nobody (on either side) gets injured or
killed. That will be an accomplishment of which we will be justly proud.

Luuk

unread,
Sep 15, 2012, 11:53:46 AM9/15/12
to
On 15-09-2012 17:34, Kenny McCormack wrote:
> In article <ksiei9-...@luuk.invalid.lan>, Luuk <lu...@invalid.lan> wrote:
>> On 15-09-2012 16:56, Manuel Collado wrote:
>>> El 15/09/2012 0:53, pop escribi�:
>>>> Ed Morton said the following on 9/14/2012 1:35 PM:
>>>>> Serious_Developer <ehabaz...@gmail.com> wrote:

>>> On Windows XP and later:

> It should be clear by now that "*nix" is completely irrelevant in the
> context of this newsgroup thread.
>

It is as irrelevant as 'On Windows XP and later'....
There is no windows before XP
(not a maintained version, at least)

pop

unread,
Sep 15, 2012, 12:04:00 PM9/15/12
to
Manuel Collado said the following on 9/15/2012 9:56 AM:
>> There is no unix/linux equivalent method to set an environment
>> variable in
>> windows as explained above. The only way I have been able to do it
>> (within
>> [g]awk) is as follows:
>>
>> set x=7
>> echo %x%
>> 7
>> gawk "BEGIN{ x=8;print \"@set x=\"x >\"setx.bat\" }"
>> call setx
>> echo %x%
>> 8
>>
>> Note that "call" is used if in a batch file and further batch statements
>> are present since a batch file is immediately exited without the "call".
>>
>> HTH
>> pop is Mark
>
> On Windows XP and later:
>
> awk "BEGIN {exit 8}"
> set x=%ERRORLEVEL%
> echo %x%
> 8
>
This only works for numerical values 0-255; other method will work for
all (legal) numerical values as well as text.

regards;
pop->Mark

pop

unread,
Sep 15, 2012, 12:09:29 PM9/15/12
to
pop said the following on 9/15/2012 11:04 AM:
I stand corrected on the numerical values... apparently "exit number"
works for any legal number...

pop->Mark

Luuk

unread,
Sep 15, 2012, 1:05:39 PM9/15/12
to
as long as its smaller than: 2147483648

Serious_Developer

unread,
Sep 15, 2012, 2:25:37 PM9/15/12
to
Just is very strange from inside the sub-batch file the value is being
set like :
print "set codein="codeawk" " > "setx.bat"
print "@echo setx.bat has been called"> "setx.bat" # that is results
correct value codeawk


from the main.bat
----------------
call setx.bat :: Does not yield the previous answer .

Though I am setting a variable with a value to the whole system ......
STRANGE ......

Any suggestions

Manuel Collado

unread,
Sep 15, 2012, 2:52:14 PM9/15/12
to
El 15/09/2012 18:09, pop escribiᅵ:
If string values must also been supported, the there is a tricky solution
based on command expansions in FOR lists:

for /F %%k in ('awk "BEGIN {print \"alpha\"}"') do set x=%%k
echo %x%
alpha

The above works for a script in a file. At the interactive console just
type %k instead of %%k.
0 new messages