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
- forward slashes
\
in windows path should be converted to/
; - cannot call Windows system
dir
command.
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? – ShiflettRS="\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. – StogyC:\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")
returns1
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 likecall "C:\cygwin64\bin\gawk.exe" -f script.awk
and version, system, print call asscript.awk
content? – Shiflettgawk.exe
in Windows batch. IncludingRS="\r\n";
and other common options for Windows usage? – Shiflett