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
问题描述
我正在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使用 Cygwin s gawk.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从批处理调用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:\cygwin64\bin\gawk.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:\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路径中的code> \ 应该转换为
/
; - 无法调用Windows系统
dir
命令。
- forward slashes
\
in windows path should be converted to/
; - 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-cygwin-to-print-fields-of-an-excel-spreadsheet 中的操作方法,即从cygwin bash脚本中调用Windows命令(Excel)。
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屋!