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

隐身守侯 提交于 2019-12-24 22:25:14

问题


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.

回答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.



来源:https://stackoverflow.com/questions/58878922/gawk-3-1-6-1-on-windows-7-x64-pro-gets-0-return-code-using-system-even-on-fail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!