Windows 7 x64 Pro 上的 gawk 3.1.6-1 使用 system() 获取 0 return 代码,即使命令失败
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,一切正常。
我的环境已更改为 Windows 7 x64,现在 gawk 3.1.4 经常因致命错误而失败。
我已经更新到最新可用的 gawk 3.1.6-1 (https://sourceforge.net/projects/gnuwin32/files/gawk/) --> 致命错误消失了 (yahoo),但是 一个非常奇怪的错误我遇到的行为:命令失败时无法获得非零 return 代码。
比如我调用
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 版本 returns 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
我所有的命令成功状态都被这个原因完全打破了。我需要非零 return 代码来表示 gawk 中失败的命令。
有人 运行 呆呆地看着 Windows 7 x64 吗?你有类似的东西吗?有什么办法可以解决这个问题吗?
UPD:给遇到同样问题并想尝试 Cygwin 的人的一些注意事项
感谢@EdMorton Cygwin 的 gawk.exe
使用理念。是的,一般来说它可以按预期在 Windows 7 x64 和 system( "exit 1")
returns 1
上运行(请参阅下面的 MWE),但是从 3.1.6
到 Cygwin 的更新不是没有痛苦的。我在想我应该在我当前的 gawk-scripts-windows-world 中对抗它们,还是在 Python 3.[=29= 中重写]
这是 Cygwin 从批处理中调用 gawk 的最小工作示例,两个脚本:
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 路径中的正斜杠
\
应转换为 /
;
- 无法调用Windows系统
dir
命令。
这是一个示例,说明如何从 Windows 调用 cygwin 的 awk。在 Windows 中,以通常的方式将“.bash”文件后缀与 "bash.exe" 相关联(创建 "test.bash" 然后右键单击打开方式并找到 bash.exe 在你的 cygwin64 目录下),然后双击一个名为 "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
它会弹出 window 显示以下内容 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)。
我正在 运行 在 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/) --> 致命错误消失了 (yahoo),但是 一个非常奇怪的错误我遇到的行为:命令失败时无法获得非零 return 代码。
比如我调用
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 版本 returns 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
我所有的命令成功状态都被这个原因完全打破了。我需要非零 return 代码来表示 gawk 中失败的命令。
有人 运行 呆呆地看着 Windows 7 x64 吗?你有类似的东西吗?有什么办法可以解决这个问题吗?
UPD:给遇到同样问题并想尝试 Cygwin 的人的一些注意事项
感谢@EdMorton Cygwin 的 gawk.exe
使用理念。是的,一般来说它可以按预期在 Windows 7 x64 和 system( "exit 1")
returns 1
上运行(请参阅下面的 MWE),但是从 3.1.6
到 Cygwin 的更新不是没有痛苦的。我在想我应该在我当前的 gawk-scripts-windows-world 中对抗它们,还是在 Python 3.[=29= 中重写]
这是 Cygwin 从批处理中调用 gawk 的最小工作示例,两个脚本:
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 路径中的正斜杠
\
应转换为/
; - 无法调用Windows系统
dir
命令。
这是一个示例,说明如何从 Windows 调用 cygwin 的 awk。在 Windows 中,以通常的方式将“.bash”文件后缀与 "bash.exe" 相关联(创建 "test.bash" 然后右键单击打开方式并找到 bash.exe 在你的 cygwin64 目录下),然后双击一个名为 "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
它会弹出 window 显示以下内容 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
另请参阅