即使在失败的命令上,Windows 7 x64 Pro 上的 gawk 3.1.6-1 也使用 system() 获得 0 返回码 [英] gawk 3.1.6-1 on Windows 7 x64 Pro gets 0 return code using system() even on failed commands

查看:19
本文介绍了即使在失败的命令上,Windows 7 x64 Pro 上的 gawk 3.1.6-1 也使用 system() 获得 0 返回码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 上运行 gawk 脚本.很长一段时间以来,我在 Windows XP x86 上使用了 gawk 3.1.4,一切正常.

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.

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

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

我已更新到最新可用的 gawk 3.1.6-1 (https://sourceforge.net/projects/gnuwin32/files/gawk/) --> 致命错误消失了(雅虎),但是我遇到了一个非常奇怪的行为:它无法获得非零回报失败命令的代码.

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.

例如,我打电话

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

为什么 2 返回:0???以前的 gawk 版本按预期返回 1

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

我所有的命令成功状态都被这个原因完全破坏了.我需要为 gawk 中失败的命令提供非零返回码.

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

有人在 Windows 7 x64 上运行 gawk 吗?你得到类似的东西吗?有什么办法可以解决这个问题吗?

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

感谢@EdMorton 和 Cygwingawk.exe 使用理念.是的,一般来说它适用于 Windows 7 x64 并且 system("exit 1") 按预期返回 1(请参阅下面的 MWE),但是 3.1 的更新.6 到 Cygwin 并非没有痛苦.我在想我应该在当前的 gawk-scripts-windows-world 中与它们作斗争,还是在 Python 3 中重写.

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.

这是 Cygwin 的来自 Batch 的 gawk 调用的最小工作示例,两个脚本:

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

REM awkTest.cmd
@echo off
set "exeGAWK=C:cygwin64ingawk.exe"
echo exeGAWK = "%exeGAWK%"

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

# 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;
}

结果是

exeGAWK = "C:cygwin64ingawk.exe"
Check version:
C:cygwin64ingawk.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

明显的问题是

  1. windows路径中的正斜杠应该转换为/;
  2. 无法调用 Windows 系统 dir 命令.
  1. forward slashes in windows path should be converted to /;
  2. cannot call Windows system dir command.

推荐答案

这里有一个示例,说明如何从 Windows 调用 cygwin 的 awk.在 Windows 中,以通常的方式将.bash"文件后缀与bash.exe"相关联(创建test.bash"然后右键单击打开方式并在您的 cygwin64 目录下找到 bash.exe)然后双击在一个名为test.bash"的文件上包含这个:

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

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

另见 how-do-i-use-awk-under-cygwin-to-print-fields-from-an-excel-spreadsheet 了解如何做相反的事情,即从 cygwin 调用 Windows 命令 (Excel)bash 脚本.

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.

这篇关于即使在失败的命令上,Windows 7 x64 Pro 上的 gawk 3.1.6-1 也使用 system() 获得 0 返回码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆