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

问题描述 投票:0回答:1

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

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

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

例如,我打电话

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 returned: 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中失败的命令,我需要非零返回码。

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

awk windows-7 64-bit windows-7-x64
1个回答
0
投票

这是您如何从Windows调用cygwin的awk的示例。在您的cygwin目录下将“ .bash”前缀与“ bash.exe”关联,然后双击包含以下内容的名为“ hello.bash”的文件:

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

# cd to your home directory in cygwin if you want to run from there
dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
dir="C:${dir#/cygdrive/c}"
cd "$dir" || exit 1

awk 'BEGIN{print "hello world"}'
sleep 10

它将弹出一个显示“ hello world”的窗口,持续10秒钟。

© www.soinside.com 2019 - 2024. All rights reserved.