Windows 7 x64 Pro上的gawk 3.1.6-1即使在命令失败时也会使用system()获取0返回代码

Windows 7 x64 Pro上的gawk 3.1.6-1即使在命令失败时也会使用system()获取0返回代码,awk,windows-7,64-bit,windows-7-x64,Awk,Windows 7,64 Bit,Windows 7 X64,我正在Windows上运行gawk脚本。很长一段时间以来,我一直在WindowsXPx86上使用Gawk3.1.4,一切正常 我的环境已更改为Windows 7 x64,现在gawk 3.1.4经常失败并出现致命错误 我已经更新到最新可用的gawk 3.1.6-1()-->致命错误消失了(yahoo),但我遇到了一个非常奇怪的行为:它无法在命令失败时获得非零返回代码 例如,我打电话 print "System return test: "; system( "gawk --version");

我正在Windows上运行gawk脚本。很长一段时间以来,我一直在WindowsXPx86上使用Gawk3.1.4,一切正常

我的环境已更改为Windows 7 x64,现在gawk 3.1.4经常失败并出现致命错误

我已经更新到最新可用的gawk 3.1.6-1()-->致命错误消失了(yahoo),但我遇到了一个非常奇怪的行为:它无法在命令失败时获得非零返回代码

例如,我打电话

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

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
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
为什么
2返回:0
???以前的gawk版本按预期返回
1

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
由于这个原因,我所有的命令成功状态都被完全破坏了。我需要gawk中失败命令的非零返回码

有人在Windows7x64上运行gawk吗?你有类似的东西吗?有办法解决这个问题吗


UPD:为那些遇到同样问题并想尝试Cygwin的人提供一些提示 感谢@EdMorton的
gawk.exe
使用理念。是的,一般来说,它在Windows 7 x64上工作,并且
系统(“退出1”)
按预期返回
1
(请参见下面的MWE),但从
3.1.6
到Cygwin的更新并非无痛。我在想,我应该在我目前的gawk脚本windows世界中与它们作斗争,还是用Python 3重写它们

这是Cygwin从批处理(两个脚本)调用gawk的一个最简单的工作示例:

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

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

结果是

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
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
显而易见的问题是

  • windows路径中的前斜杠
    \
    应转换为
    /
  • 无法调用Windows系统
    dir
    命令

  • 下面是一个如何从Windows调用cygwin的awk的示例。在Windows中,以通常的方式将“.bash”文件后缀与“bash.exe”关联(创建“test.bash”,然后右键单击打开并在cygwin64目录下找到bash.exe),然后双击名为“test.bash”的文件,其中包含以下内容:

    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
    
    它将弹出一个窗口,显示以下内容30秒:

    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
    

    另请参见如何执行相反的操作,即从cygwin bash脚本调用Windows命令(Excel)。

    可能是个bug,idk。或者可能是你参考的那个下载文件夹说它是32位Windows的,而你是64位的。在任何情况下,Gawk3.1.6都落后于2个完整版本,大约过期5年。为什么不安装cygwin并运行gawk 5.0.1呢?gawk 5.0.1有一些错误修复和大量附加功能(并且可以按照您的要求运行)?@Ed Morton,我已经准备好更新并尝试任何更新的版本,但3.1.6是我为Windows找到的最新版本。cygwin的gawk是否可以在Windows批处理(cmd.exe)环境中的cygwin外壳外部使用?您可以从Windows批处理命令调用cygwin的bash.exe,然后从该命令调用cygwin gawk。我刚刚发布了一个答案,显示了如何调用包含直接从Windows调用cygwin gawk的cygwin bash脚本。我的答案对您不起作用吗?我不太喜欢添加这样的批处理调用,因为我不知道这样做是否正确,利弊,警告等。如果您愿意,可以在问题的末尾添加一些内容。