gawk/Windows/cmd.exe/ARGV

gawk/Windows/cmd.exe/ARGV

Post by Ted Davi » Sat, 12 Dec 2009 10:04:02



You can also use which

which awk.exe > nul 2>&1
if errorlevel 1 echo not found

That does not test the current directory unless it is in the path - one
solution is the same as the *ix solution (add . to the PATH), anotehr is
to make a separate test

if exist awk.exe goto :cont
which awk.exe > nul 2>&1
if not errorlevel 1 goto :cont
goto :fail
:cont



Or just
gawk -v var="%*" "BEGIN{print var}"

Or
set var=
:loop
shift
if %1!==! goto :cont
set var=%var% %1
goto :loop
:cont
gawk -v var="%var%" "BEGIN{print var}"

In FOR commands, it is customary, and sometimes necessary to start with
%%A, and often necessary to avoid punctuation for variables (it is
undocumented).



Or
gawk -v var="%1 %2 %3" "BEGIN{print var}"


Note that quoted batch arguments work in gawk (the extra "" are stripped
off), but I don't know about other ports.

alt.msdos.batch.nt is the group for NT/XP/Vista/Win7 batch topics.

--

T.E.D. ( XXXX@XXXXX.COM )
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by mss » Sat, 12 Dec 2009 19:01:54


Yet in your example I note, if (say) %2 == "" then you wind up with:

'arg1arg3'

instead of:

'arg1 arg2 arg3'


The information was meant for AWK users where the coin of the realm
is typically (with respect to scripting) #!/bin/sh, and might wonder
about using AWK under Windows. Just some quick examples...

An AWK user somewhere will be able to use it - a decidedly good thing.

--
later on,
Mike

http://www.yqcomputer.com/

 
 
 

gawk/Windows/cmd.exe/ARGV

Post by Ted Davi » Sun, 13 Dec 2009 00:54:55


Actually, that should have been %2 %3 %4, but it makes no difference, your
code and mine both crash gawk (at least some versions) if one of the args
is "". I don't think empty quotes as an argument has ever been discussed
in the batch group.

Note that echo[ or echo. are the preferred methods of generating a blank
line.


That was for *your* benefit - that's the place to learn about batch
language, including using it in conjuntion with awk.


--
T.E.D. ( XXXX@XXXXX.COM )
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by gazell » Sun, 13 Dec 2009 01:10:37


...

To which, Ted replied, somewhat curtly:

This interaction illustrates well the fact that newsgroups (*) are not a
good medium for providing unsolicited help. That is, if you break the
established "newbie posts question, oldtimer gives answer" (rinse,
lather, repeat) paradigm, people get confused as to what their roles are.

Another way of looking at this is that the oldtimer always assumes that
there is some, possibly hidden, question inside the newbie's seemingly
"helpful" post - and he (the oldtimer) will try to ferret out what "the
real question" is (aka, "What problem are we trying to solve?").

Newsgroup dynamics. Gotta love 'em...!

=========================================================================
(*) And other online support fora, in general.
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by mss » Sun, 13 Dec 2009 03:28:56


Can you post an example of my code crashing under gawk?


Yes certainly, thanks Ted, I appreciate your help and insight.
Wasn't at all tiring to make light of your advice but rather clarify...

--
later on,
Mike
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by Ted Davi » Sun, 13 Dec 2009 06:17:13


for %%x in (%2 %3 %4) do (
if not [%%x]==[] call set ex3=%ex3% %%x
)
echo | gawk -v var="%ex3%" "END{print var}"


Actually, I corrected the syntax error %% instead of % for environment
variables, but it crashes either way (screen dump):

d:\myfiles>test foo zap "" baz
gawk: cmd. line:1: fatal: cannot open file `zap' for reading (No such file
or directory)



I've been thinking about this issue of passing args, and I think a may
have a solution, but I can't tent to it now: i"ve got a pair of sick
laptops on the bench and 45 minutes to make them work.

--
T.E.D. ( XXXX@XXXXX.COM )
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by Tim Menzie » Sun, 13 Dec 2009 07:22:44

would ted or mike care to sumamrize the above discussion for awk.info?
i'd do it myself but i am not a windoze man

t




> if not [%%x]==[] call set ex3=%ex3% %%x> > )> > echo | gawk -v var="%ex3%" "END{print var}"> >> > Actually, I corrected the syntax error %% instead of % for environment> > variables, but it crashes either way (screen dump):> >> > d:\myfil>s>test foo zap "" baz> > gawk: cmd. line:1: fatal: cannot open file `zap' for reading (No such file> > or directory)> >> >> >> >>>> That was for *your* benefit - that's the place to learn about batch> >>>> language, including using it in conjuntion with awk.> >> > > Yes certainly, thanks Ted, I appreciate your help and insight. Wasn't at> > > all tiring to make light of your advice but rather clarify...> >> > I've been thinking about this issue of passing args, and I think a may> > have a solution, but I can't tent to it now: i"ve got a pair of sick> > laptops on the bench and 45 minutes to make them work.> >> > --> > T.E.D. ( XXXX@XXXXX.COM )
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by Ted Davi » Sun, 13 Dec 2009 10:22:03


This works:

set args=%2 %3 %4
gawk "BEGIN{print ENVIRON[\"args\"]}"

Screen dump:

c:\myfiles>test foo "bar" "baz" qux
"bar" "baz" qux

--

T.E.D. ( XXXX@XXXXX.COM )
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by mss » Sun, 13 Dec 2009 10:28:21


Wait, that changes things Ted. Using %% will preserve some of the
more *** strings. That was on purpose. Whether or not its an
error is relative to its intended context...

Using the example I posted:

for %%x in (%2 %3 %4) do (
if not [%%x]==[] call set ex3=%%ex3%% %%x
)
gawk -v var="%ex3%" "BEGIN{print var}"

When invoking: 'test foo zap "" baz'

The result here is:

gawk: baz BEGIN{print
gawk: ^ syntax error
gawk: cmd. line:1: baz BEGIN{print
gawk: cmd. line:1: ^ unexpected newline or end of string
errcount: 2

So... (confusedly) are you tiring to explain that the shell is
expanding '""' as a null string? Or... perhaps that %3 lierally
equals the two characters "".

(???)


I can not reproduce the error you seem to be having on my end,
using either my code, or your modifications to it.

However... later you went on to ponder:


Well, all is correct then (as it were) in my initial post in this
thread excepting possibly the example that uses %ex3%.

If there is way better express the example in question *using only
shell constructs*, then I'd be intrested in adding it to my notes.

--
later on,
Mike

http://www.yqcomputer.com/
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by mss » Sun, 13 Dec 2009 10:33:25


Umm well... I was intending to share the results of my findings in the
spirit of learning with respect to passing arguments to AWK under a Windows
command script, and am hoping I've not instead upset the signal-to-noise
ratio of this newsgroup unwittingly. Sooo... if I'm reading both your's
and Kenny's intent properly, I'll exit stage left for this thread.

My bad folks, carry on.

--
later on,
Mike

http://www.yqcomputer.com/
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by Tim Menzie » Sun, 20 Dec 2009 11:18:06

dear mike,

if you read my message as "put a lid on it!", that was absolutely NOT
my intention.

rather, my intention was to say "way cool- i sincerely want to see a
nice summary of this on awk.info". normally, i see such summarization
as my job. but these are windoze tricks and i don't use windoze. so,
before, i was writing to ask for a little help.

in any case, i really like your code. that web server (in awk) you
wrote is just fantastic ( http://www.yqcomputer.com/
browse_thread/thread/2509ad733d6a5df2#)

so sorry*1000 if i inadvertently offended you. please keep those
cards and letters coming in!

enjoy!
:-)

t
 
 
 

gawk/Windows/cmd.exe/ARGV

Post by mss » Sun, 20 Dec 2009 23:21:06


Shoot, no biggie. I had everyone coming from (seemingly) differing
vantage points, & didn't quite know what to do. Speaking only for
myself, I'm taking an 'OS agnostic' point of view & am hoping to
code for multiple platforms =)

Such is life for a usenet newbie. But hey on the flipside Tim...
its bound to provide everyone here with some entertainment, chuckle.

later on,
Mike

--
http://www.yqcomputer.com/