I need to capture the return code of the "which" command in a Windows
command window, so I can check for success. This must be done in an awk
script running with gawk.
I tried using the system command, but apparently the value that is
stored is whether or not the system command succeeded, regardless of
what happened with which. So, if which succeeds, I get 0, and if it
fails I also get 0.
function have_program (name) {
if (match(ENVIRON["COMSPEC"], "command[.]com$")) { # win98
return 0==system("which " name " >NUL");
}
else { # XP or 2000
return 0==system("which " name " 2>&1 >NUL");
}
}
How can I capture the return code for "which" in Windows (XP) in an awk
script?
Thanks,
JP
(more familiar with perl than with awk, and only mildly so)
|
|
0
|
|
|
|
Reply
|
jeanne.petrangelo (6)
|
9/5/2006 6:05:14 PM |
|
I figured out something using getline. I'm still getting a result I
can't explain. Here's a snippet:
function have_program (name) {
code = (("which " name) | getline );
return code;
}
BEGIN {
#Testing part:
value = have_program("programB");
print value;
if (have_program("programA.exe")) {
build_program = "programA";
}
else if (have_program("programB.exe")) {
build_program = "programB";
}
else if (have_program("programC.exe")) {
build_program = "programC";
}
else {
print "pcbuild.awk: Can't find the program " > "/dev/stderr";
exit 1;
}
When I invoke this script, programB is the only one that exists. The
first time have_program is invoked (the "Testing part"), the value of
"code" within "have_program" is 1, and "have_program" returns 1, as I
expect. The second time is different: have_program returns 0 for both
programA and programB, even though the "which" command knows programB
is there (I know that because I saw the "which" stderr spewage for not
finding programA, but not for programB).
Now, if I comment out the "Testing part" and run the script again, when
the "if" chain tests for programB, have_program returns 1 as it should.
I'm quite confused.
Help?
Thanks,
JP
jeanne.petrangelo@gmail.com wrote:
> I need to capture the return code of the "which" command in a Windows
> command window, so I can check for success. This must be done in an awk
> script running with gawk.
>
> I tried using the system command, but apparently the value that is
> stored is whether or not the system command succeeded, regardless of
> what happened with which. So, if which succeeds, I get 0, and if it
> fails I also get 0.
>
> function have_program (name) {
> if (match(ENVIRON["COMSPEC"], "command[.]com$")) { # win98
> return 0==system("which " name " >NUL");
> }
> else { # XP or 2000
> return 0==system("which " name " 2>&1 >NUL");
> }
> }
>
>
> How can I capture the return code for "which" in Windows (XP) in an awk
> script?
>
> Thanks,
> JP
>
> (more familiar with perl than with awk, and only mildly so)
|
|
0
|
|
|
|
Reply
|
jeanne
|
9/5/2006 9:13:40 PM
|
|
Er, I meant to say the second time have_program is invoked, it returns
0 when I expect it to return 1.
And, even better, that led me to figure this out. I thought I only
needed to close the string that is being piped through getline if that
string is a file name, but apparently that's true for command line
strings too. If I add this line before the return in have_program, the
script does what I expect it to:
close("which " name);
Have a good one,
JP
jeanne.petrangelo@gmail.com wrote:
> I figured out something using getline. I'm still getting a result I
> can't explain. Here's a snippet:
>
> function have_program (name) {
> code = (("which " name) | getline );
> return code;
> }
>
> BEGIN {
>
> #Testing part:
> value = have_program("programB");
> print value;
>
> if (have_program("programA.exe")) {
> build_program = "programA";
> }
> else if (have_program("programB.exe")) {
> build_program = "programB";
> }
> else if (have_program("programC.exe")) {
> build_program = "programC";
> }
>
> else {
> print "pcbuild.awk: Can't find the program " > "/dev/stderr";
> exit 1;
> }
>
>
> When I invoke this script, programB is the only one that exists. The
> first time have_program is invoked (the "Testing part"), the value of
> "code" within "have_program" is 1, and "have_program" returns 1, as I
> expect. The second time is different: have_program returns 0 for both
> programA and programB, even though the "which" command knows programB
> is there (I know that because I saw the "which" stderr spewage for not
> finding programA, but not for programB).
>
> Now, if I comment out the "Testing part" and run the script again, when
> the "if" chain tests for programB, have_program returns 1 as it should.
> I'm quite confused.
>
> Help?
> Thanks,
> JP
>
>
> jeanne.petrangelo@gmail.com wrote:
> > I need to capture the return code of the "which" command in a Windows
> > command window, so I can check for success. This must be done in an awk
> > script running with gawk.
> >
> > I tried using the system command, but apparently the value that is
> > stored is whether or not the system command succeeded, regardless of
> > what happened with which. So, if which succeeds, I get 0, and if it
> > fails I also get 0.
> >
> > function have_program (name) {
> > if (match(ENVIRON["COMSPEC"], "command[.]com$")) { # win98
> > return 0==system("which " name " >NUL");
> > }
> > else { # XP or 2000
> > return 0==system("which " name " 2>&1 >NUL");
> > }
> > }
> >
> >
> > How can I capture the return code for "which" in Windows (XP) in an awk
> > script?
> >
> > Thanks,
> > JP
> >
> > (more familiar with perl than with awk, and only mildly so)
|
|
0
|
|
|
|
Reply
|
jeanne
|
9/5/2006 9:26:48 PM
|
|
jeanne.petrangelo@gmail.com wrote:
> Er,
Please don't top-post. See below.
I meant to say the second time have_program is invoked, it returns
> 0 when I expect it to return 1.
>
> And, even better, that led me to figure this out. I thought I only
> needed to close the string that is being piped through getline if that
> string is a file name, but apparently that's true for command line
> strings too. If I add this line before the return in have_program, the
> script does what I expect it to:
> close("which " name);
Normally, it's good practice to define a string to hold the full command
name and use that string for both the open and close, e.g.:
function have_program (name, code, cmd, tmp) {
cmd = "which " name
code = (cmd | getline tmp)
close(cmd)
return code;
}
By the way, note the extra unused function arguments to provide local
variables.
> Have a good one,
> JP
>
> jeanne.petrangelo@gmail.com wrote:
>
>>I figured out something using getline. I'm still getting a result I
>>can't explain. Here's a snippet:
>>
>>function have_program (name) {
>> code = (("which " name) | getline );
>> return code;
>>}
>>
>>BEGIN {
>>
>>#Testing part:
>>value = have_program("programB");
>>print value;
>>
>> if (have_program("programA.exe")) {
>> build_program = "programA";
>> }
>> else if (have_program("programB.exe")) {
>> build_program = "programB";
>> }
>> else if (have_program("programC.exe")) {
>> build_program = "programC";
>> }
>>
>> else {
>> print "pcbuild.awk: Can't find the program " > "/dev/stderr";
>> exit 1;
>> }
>>
>>
>>When I invoke this script, programB is the only one that exists. The
>>first time have_program is invoked (the "Testing part"), the value of
>>"code" within "have_program" is 1, and "have_program" returns 1, as I
>>expect. The second time is different: have_program returns 0 for both
>>programA and programB, even though the "which" command knows programB
>>is there (I know that because I saw the "which" stderr spewage for not
>>finding programA, but not for programB).
"code" contains the return code from "getline", not the return code from
"which".
>>Now, if I comment out the "Testing part" and run the script again, when
>>the "if" chain tests for programB, have_program returns 1 as it should.
>> I'm quite confused.
>>
>>Help?
>>Thanks,
>>JP
>>
>>
>>jeanne.petrangelo@gmail.com wrote:
>>
>>>I need to capture the return code of the "which" command in a Windows
>>>command window, so I can check for success. This must be done in an awk
>>>script running with gawk.
>>>
>>>I tried using the system command, but apparently the value that is
>>>stored is whether or not the system command succeeded, regardless of
>>>what happened with which. So, if which succeeds, I get 0, and if it
>>>fails I also get 0.
>>>
>>>function have_program (name) {
>>> if (match(ENVIRON["COMSPEC"], "command[.]com$")) { # win98
>>> return 0==system("which " name " >NUL");
>>> }
>>> else { # XP or 2000
>>> return 0==system("which " name " 2>&1 >NUL");
>>> }
>>>}
>>>
>>>
>>>How can I capture the return code for "which" in Windows (XP) in an awk
>>>script?
>>>
>>>Thanks,
>>>JP
>>>
>>>(more familiar with perl than with awk, and only mildly so)
>
When I read this I thought you were looking for the exit code from
"which", but apparently all you actually want is it's output, which is
an easier task. Given that, either save the output in a file then read
it back, e.g.:
system("command > file")
getline result < "file"
or use getline from a pipe as you discovered, e.g.:
"command" | getline result
close("command")
or use coprocesses, e.g.:
print "input" |& "command"
"command" |& getline result
Just beware of using getline as it has a ton of caveats (including the
one that led me to use "tmp" as an argument). You really should get the
Gawk book (http://www.oreilly.com/catalog/awkprog3/) if you're going to
be using awk a lot.
Regards,
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
9/5/2006 10:01:07 PM
|
|
[order cleaned up to fix top-posting; my apologies; and un-needed lines
snipped]
Ed Morton wrote:
> jeanne.petrangelo@gmail.com wrote:
> >
> > jeanne.petrangelo@gmail.com wrote:
> >>jeanne.petrangelo@gmail.com wrote:
> >>
> >>>I need to capture the return code of the "which" command in a Windows
> >>>command window, so I can check for success. This must be done in an awk
> >>>script running with gawk.
> >>>
> >>>I tried using the system command, but apparently the value that is
> >>>stored is whether or not the system command succeeded, regardless of
> >>>what happened with which. So, if which succeeds, I get 0, and if it
> >>>fails I also get 0.
[snip]
> >
> >>I figured out something using getline. I'm still getting a result I
> >>can't explain. Here's a snippet:
> >>
> >>function have_program (name) {
> >> code = (("which " name) | getline );
> >> return code;
> >>}
> >>
> >>BEGIN {
> >>
> >>#Testing part:
> >>value = have_program("programB");
> >>print value;
> >>
> >> if (have_program("programA.exe")) {
> >> build_program = "programA";
> >> }
> >> else if (have_program("programB.exe")) {
> >> build_program = "programB";
> >> }
> >> else if (have_program("programC.exe")) {
> >> build_program = "programC";
> >> }
> >>
> >> else {
> >> print "pcbuild.awk: Can't find the program " > "/dev/stderr";
> >> exit 1;
> >> }
> >>
> >>> >>When I invoke this script, programB is the only one that exists. The
> >>first time have_program is invoked (the "Testing part"), the value of
> >>"code" within "have_program" is 1, and "have_program" returns 1, as I
> >>expect. The second time is different: have_program returns 0 for both
> >>programA and programB, even though the "which" command knows programB
> >>is there (I know that because I saw the "which" stderr spewage for not
> >>finding programA, but not for programB).
>
> "code" contains the return code from "getline", not the return code from
> "which".
Ah, so the return code from "getline" does tell me whether or not
"which" found the command, because when "which" is successful, it
outputs to sdtout, and when it isn't the output goes to stderr.
(guessing.) So "getline" returns a 1 if "which" found the command, and
0 otherwise. This is the behavior I see, anyway.
> Normally, it's good practice to define a string to hold the full command
> name and use that string for both the open and close, e.g.:
>
> function have_program (name, code, cmd, tmp) {
> cmd = "which " name
> code = (cmd | getline tmp)
> close(cmd)
> return code;
> }
>
> By the way, note the extra unused function arguments to provide local
> variables.
Thank you for the tip! I will put it to use.
>
> When I read this I thought you were looking for the exit code from
> "which", but apparently all you actually want is it's output, which is
> an easier task. Given that, either save the output in a file then read
> it back, e.g.:
[snip]
Actually, your first read was correct. I do not need to know the
location of a command, just whether or not it is found. I appreciate
the advice; I may be able to use it some day.
> Just beware of using getline as it has a ton of caveats (including the
> one that led me to use "tmp" as an argument). You really should get the
> Gawk book (http://www.oreilly.com/catalog/awkprog3/) if you're going to
> be using awk a lot.
Thank you for this pointer as well. At the moment, I'm just a C
programmer trying to fix a broken build environment, and awk is the
best tool for my immediate purpose. Now that I've been introduced to
the language, it's nice to have it in my toolbox.
Have a good one,
JP
|
|
0
|
|
|
|
Reply
|
jeanne
|
9/6/2006 2:55:10 PM
|
|
In article <1157554510.788521.218630@p79g2000cwp.googlegroups.com>,
<jeanne.petrangelo@gmail.com> wrote:
....
>> When I read this I thought you were looking for the exit code from
>> "which", but apparently all you actually want is it's output, which is
>> an easier task. Given that, either save the output in a file then read
>> it back, e.g.:
>[snip]
>
>Actually, your first read was correct. I do not need to know the
>location of a command, just whether or not it is found. I appreciate
>the advice; I may be able to use it some day.
Actually, I think what's really going on here is that getline returns an
immediate EOF (0) if there is no stdout output from the command (which
is the case when the command is "not found"). If there is something to
read, then it returns 1.
I don't think there is any "standard" way using getline to get the exit
value of the command.
But, system works fine for me, using GAWK:
% gawk 'BEGIN {print system("which dslkjfls")}'
1
% gawk 'BEGIN {print system("which ls")}'
/bin/ls
0
Are you sure these aren't the results you would get?
|
|
0
|
|
|
|
Reply
|
gazelle
|
9/6/2006 5:48:21 PM
|
|
Kenny McCormack wrote:
> In article <1157554510.788521.218630@p79g2000cwp.googlegroups.com>,
>
> I don't think there is any "standard" way using getline to get the exit
> value of the command.
>
> But, system works fine for me, using GAWK:
>
> % gawk 'BEGIN {print system("which dslkjfls")}'
> 1
> % gawk 'BEGIN {print system("which ls")}'
> /bin/ls
> 0
>
> Are you sure these aren't the results you would get?
Yes, I'm certain. My system call returns 0 whether the command is
present or not. I read in an old post that this is a behavior that is
different between Unix and Windows, and I'm running this on Windows.
(sorry, I can't do much about that)
JP
|
|
0
|
|
|
|
Reply
|
jeanne
|
9/6/2006 6:29:39 PM
|
|
In article <1157567379.455963.219140@p79g2000cwp.googlegroups.com>,
<jeanne.petrangelo@gmail.com> wrote:
>
>Kenny McCormack wrote:
>> In article <1157554510.788521.218630@p79g2000cwp.googlegroups.com>,
>>
>> I don't think there is any "standard" way using getline to get the exit
>> value of the command.
>>
>> But, system works fine for me, using GAWK:
>>
>> % gawk 'BEGIN {print system("which dslkjfls")}'
>> 1
>> % gawk 'BEGIN {print system("which ls")}'
>> /bin/ls
>> 0
>>
>> Are you sure these aren't the results you would get?
>
>Yes, I'm certain. My system call returns 0 whether the command is
>present or not. I read in an old post that this is a behavior that is
>different between Unix and Windows, and I'm running this on Windows.
>(sorry, I can't do much about that)
Yes, you are right. I looked back at your original post and you did
specify Windows (quite often here, people don't, and the responses tend
to assume Unix unless/until the OP says, "Oh, and I'm running on
Windows...").
Given that you now have a solution using getline (although Cnot for the
reason you originally thought - i.e., you still aren't getting the exit
status of the command), this is now probably moot for you, but I should
also add that this is not strictly speaking a different between Unix and
Windows, but rather a function of what shell you are using (i.e., which
shell is being invoked by GAWK to run system commands). It's a shame
(but not a surprise...) that none of the standard DOS/Windows shells
work right.
There are shells available for DOS/Windows that do preserve the exit
status. They are hard to find, though...
BTW, which version of Windows are you using (the DOS/95/95/ME strain ror
the NT/2K/XP strain) ? The shells in the later do work somewhat better
than those in the former.
|
|
0
|
|
|
|
Reply
|
gazelle
|
9/6/2006 7:03:18 PM
|
|
Kenny McCormack wrote:
[snip]
>
> There are shells available for DOS/Windows that do preserve the exit
> status. They are hard to find, though...
>
> BTW, which version of Windows are you using (the DOS/95/95/ME strain ror
> the NT/2K/XP strain) ? The shells in the later do work somewhat better
> than those in the former.
Win XP.
We do embedded programming using a cross compiler that runs on Windoze.
Awk is part of our standard development environment, which in installed
on all the developer PCs and distributed to our co-dev partners. For
some reason, perl is not. The script will be invoked from within a
makefile.
Here's another, somewhat related, question:
Can I surpress the output of the "which" command to the console while
maintaining the functionality for the script? Right now, every time the
"which" command comes up empty handed, the console shows "which"s
complaint about not finding the command followed by all the environment
path info. It's not critical, but it's annoying.
I've tried variations on ">NUL 2>&1" which I found around, with various
placements in the line "code = (cmd | getline tmp);" but I either get
no change in output or syntax errors that prevent the script from
running. I believe that redirects stdout to NUL and stderr to stdout.
Thanks,
JP
|
|
0
|
|
|
|
Reply
|
jeanne
|
9/6/2006 8:23:36 PM
|
|
In article <1157574216.463990.39380@b28g2000cwb.googlegroups.com>,
<jeanne.petrangelo@gmail.com> wrote:
....
>Here's another, somewhat related, question:
>Can I surpress the output of the "which" command to the console while
>maintaining the functionality for the script? Right now, every time the
>"which" command comes up empty handed, the console shows "which"s
>complaint about not finding the command followed by all the environment
>path info. It's not critical, but it's annoying.
>
>I've tried variations on ">NUL 2>&1" which I found around, with various
>placements in the line "code = (cmd | getline tmp);" but I either get
>no change in output or syntax errors that prevent the script from
>running. I believe that redirects stdout to NUL and stderr to stdout.
I think "2>nul" should do the trick (leaving stdout undisturbed).
|
|
0
|
|
|
|
Reply
|
gazelle
|
9/6/2006 9:50:36 PM
|
|
jeanne.petrangelo@gmail.com escribi�:
> Kenny McCormack wrote:
> [snip]
>
> I've tried variations on ">NUL 2>&1" which I found around, with various
> placements in the line "code = (cmd | getline tmp);" but I either get
> no change in output or syntax errors that prevent the script from
> running. I believe that redirects stdout to NUL and stderr to stdout.
Sorry, not. IIRC, this redirects stdout to NUL and then stderr to
wherever stdout is currently redirected , i.e. also to NUL.
Regards.
--
To reply by e-mail, please remove the extra dot
in the given address: m.collado -> mcollado
|
|
0
|
|
|
|
Reply
|
Manuel
|
9/7/2006 11:01:09 AM
|
|
|
10 Replies
355 Views
(page loaded in 0.147 seconds)
|