gawk 3.1.6-1 on Windows 7 x64 Pro gets 0 return code using system() even on failed commands
Asked Answered
S

1

0

I'm running gawk scripts on Windows. For a long time I've used gawk 3.1.4 on Windows XP x86 and all was OK.

My environment has changed to Windows 7 x64, and now gawk 3.1.4 frequently fails with fatal errors.

I've updated to latest available gawk 3.1.6-1 (https://sourceforge.net/projects/gnuwin32/files/gawk/) --> fatal errors are gone (yahoo), but a very strange behaviour I met: it cannot get non-zero return code on failing command.

For example, I call

print "System return test: ";
system( "gawk --version");
myReturnCode = system( "exit 0");
print "1 returned: " myReturnCode;
myReturnCode = system( "exit 1");
print "2 returned: " myReturnCode;

and the result is

System return test:
GNU Awk 3.1.6
Copyright (C) 1989, 1991-2007 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
1 returned: 0
2 returned: 0

Why 2 returned: 0??? Previous gawk versions returns 1 as expected

System return test:
GNU Awk 3.1.4
Copyright (C) 1989, 1991-2003 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
1 returned: 0
2 returned: 1

All my command success statuses are totally broken by this reason. I need non-zero return codes for the failed commands in gawk.

Does anybody run gawk on Windows 7 x64? Do you get something similar? Is there any ways to work this problem out?


UPD: Some notes for those who met the same problem and would like to try Cygwin

Thanks to @EdMorton with Cygwin's gawk.exe usage idea. Yes, generally speaking it works on Windows 7 x64 and system( "exit 1") returns 1 as expected (see MWE below), but the update from 3.1.6 to Cygwin is not painless. And I'm thinking should I fight against them in my current gawk-scripts-windows-world, or rewrite in in Python 3.

This is a minimal working example of Cygwin's gawk call from a Batch, two scripts:

REM awkTest.cmd
@echo off
set "exeGAWK=C:\cygwin64\bin\gawk.exe"
echo exeGAWK = "%exeGAWK%"

call "%exeGAWK%" -f "test.awk" nul

and

# test.awk
END\
{
    exeGAWK = ENVIRON[ "exeGAWK" ];

    print "Check version: ";

    print exeGAWK
    system( exeGAWK " --version");
    gsub(/\\/, "/", exeGAWK)
    print exeGAWK
    system( exeGAWK " --version");


    print "Dir test: ";
    system( "dir " exeGAWK);

    print "System return test: ";
    myReturnCode = system( "exit 0");
    print "1 returned: " myReturnCode;
    myReturnCode = system( "exit 1");
    print "2 returned: " myReturnCode;
}

The result is

exeGAWK = "C:\cygwin64\bin\gawk.exe"
Check version:
C:\cygwin64\bin\gawk.exe
sh: C:cygwin64bingawk.exe: command not found
C:/cygwin64/bin/gawk.exe
GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
Dir test:
sh: dir: command not found
System return test:
1 returned: 0
2 returned: 1

The obvious problems are

  1. forward slashes \ in windows path should be converted to /;
  2. cannot call Windows system dir command.
Shiflett answered 15/11, 2019 at 14:20 Comment(9)
Maybe it's a bug, idk. Or maybe it's that that download folder you reference says it's for 32 bit Windows and you're on 64 bit. In any case gawk 3.1.6 is 2 full releases behind and about 5 years out of date. Why not install cygwin and run gawk 5.0.1 which has some bug fixes and a TON of additional functionality (and behaves as you want)?Stogy
@Ed Morton, I'm ready to update and try any newer release but 3.1.6 was the latest that I found for Windows. Does cygwin's gawk could be used outside cygwin shell in Windows batch (cmd.exe) environment?Shiflett
You can call cygwin's bash.exe from a Windows batch command and call cygwin gawk from that. I just posted an answer showing how to call a cygwin bash script containing a call to cygwin gawk directly from Windows.Stogy
did my answer not work for you?Stogy
@Ed Morton, thank you for the response. I had no time on the weekend. I will try to call cygwin's gawk from batch tomorrow. So I expect to call from batch something like call ".\path\to\bash.exe" "./path/to/awk" -f "./path/to/myScript.awk". I also suppose that some problems with CR LF and Windows path inside awk-scripts could occur, right?Shiflett
You can work around DOS line endings just by setting RS="\r\n" in the BEGIN section of the awk script and you can see how both answers are manipulating paths to resolve any Windows vs Cygwin issues. Literally all I know about batch scripts is what I managed to cobble together from google to create the script referenced at the end of my answer below so I can't help you much more with that, sorry.Stogy
@EdMorton, I've installed Cygwin today. Looks like the direct batch call works C:\cygwin64\bin\gawk.exe --version as expected: GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2) .... And, of course, system( "exit 1") returns 1 as expected! So I will accept you answer as a bug solution. About Win call, would you update your answer with a minimal working example (MWE) of a batch call (now it is bash script) command like call "C:\cygwin64\bin\gawk.exe" -f script.awk and version, system, print call as script.awk content?Shiflett
Or I should do it myself and post as another answer / or add it as UPD to the question for the other users as a example of usage cygwin's gawk.exe in Windows batch. Including RS="\r\n"; and other common options for Windows usage?Shiflett
I don't feel comfortable adding a batch call like that since I've no idea if that's the right way to do it, pros/cons, caveats, etc. You could add something to the end of your question if you like.Stogy
S
1

Here's an example of how you can call cygwin's awk from Windows. In Windows associate the ".bash" file suffix with "bash.exe" in the usual manner (create "test.bash" then right-click to open-with and find the bash.exe under your cygwin64 directory) and then double click on a file named "test.bash" containing this:

export HOME="/cygdrive/c/cygwin64/home/$USERNAME"
export PATH="$HOME:/usr/bin:$PATH"
. .bash_profile

# cd to the directory this script is in assuming you want it to run from there
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
dir="C:${dir#/cygdrive/c}"
cd "$dir" || exit 1

awk 'BEGIN {
    print "System return test: ";
    system( "gawk --version");
    myReturnCode = system( "exit 0");
    print "1 returned: " myReturnCode;
    myReturnCode = system( "exit 1");
    print "2 returned: " myReturnCode;
}'

sleep 30

and it'll pop up a window displaying the following for 30 seconds:

System return test:
GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2019 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
1 returned: 0
2 returned: 1

See also how-do-i-use-awk-under-cygwin-to-print-fields-from-an-excel-spreadsheet for how to do the opposite, i.e. call a Windows command (Excel) from a cygwin bash script.

Stogy answered 15/11, 2019 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.