Perl: 系统 return 代码是 256 而不是 0
Perl: system return code is 256 instead of 0
我有一个 oneliner(Windows 7/10 上的 perl 5):
C:\Windows>perl -e "$x=system('echo c:\users');print $x";
这给了我预期的输出:
c:\users
0
但是如果我有这个:
C:\Windows>perl -e "$x=system('explorer c:\users');print $x";
这给了我:
256
并打开文件夹。
我很好奇为什么命令正确执行后输出不是0。
类似的问题是here,但没有人回答。这个问题在这里更具体。也许有人可以回答这个具体问题。
更新 1:
(1) 我将路径从 c:\user 更正为 c:\users。之前的路径错误,文件资源管理器 C:\Users\giordano\Documents 被打开。通过此更正,正确的文件夹是 openend 但仍然是 returns 256.
(2) 更好的编码:
C:\Windows>perl -e "system('explorer c:\users');print $?";
The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below).
在 $?
中设置了相同的值,其中 documents this further:
This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ($? >> 8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump.
另请注意,如果出现 fork 或 exec 错误(在这种情况下,errno 设置在 $!
中),则可以将其中一个设置为 -1。
所以这意味着当执行你的第二个命令时,它指示退出状态 1 (256 >> 8)。我不知道在 Windows.
的上下文中这意味着什么
我有一个 oneliner(Windows 7/10 上的 perl 5):
C:\Windows>perl -e "$x=system('echo c:\users');print $x";
这给了我预期的输出:
c:\users
0
但是如果我有这个:
C:\Windows>perl -e "$x=system('explorer c:\users');print $x";
这给了我:
256
并打开文件夹。
我很好奇为什么命令正确执行后输出不是0。
类似的问题是here,但没有人回答。这个问题在这里更具体。也许有人可以回答这个具体问题。
更新 1:
(1) 我将路径从 c:\user 更正为 c:\users。之前的路径错误,文件资源管理器 C:\Users\giordano\Documents 被打开。通过此更正,正确的文件夹是 openend 但仍然是 returns 256.
(2) 更好的编码:
C:\Windows>perl -e "system('explorer c:\users');print $?";
The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below).
在 $?
中设置了相同的值,其中 documents this further:
This is just the 16-bit status word returned by the traditional Unix wait() system call (or else is made up to look like it). Thus, the exit value of the subprocess is really ($? >> 8), and $? & 127 gives which signal, if any, the process died from, and $? & 128 reports whether there was a core dump.
另请注意,如果出现 fork 或 exec 错误(在这种情况下,errno 设置在 $!
中),则可以将其中一个设置为 -1。
所以这意味着当执行你的第二个命令时,它指示退出状态 1 (256 >> 8)。我不知道在 Windows.
的上下文中这意味着什么