ruby 系统命令检查退出代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18728069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 06:09:48  来源:igfitidea点击:

ruby system command check exit code

rubycommandexitexit-code

提问by user1530318

I have a bunch of system calls in ruby such as the following and I want to check their exit codes simultaneously so that my script exits out if that command fails.

我在 ruby​​ 中有一堆系统调用,如下所示,我想同时检查它们的退出代码,以便我的脚本在该命令失败时退出。

system("VBoxManage createvm --name test1")
system("ruby test.rb")

I want something like

我想要类似的东西

system("VBoxManage createvm --name test1", 0)<-- where the second parameter checks the exit code and confirms that that system call was successful, and if not, it'll raise an error or do something of that sort.

system("VBoxManage createvm --name test1", 0)<-- 其中第二个参数检查退出代码并确认该系统调用成功,如果没有,它将引发错误或执行此类操作。

Is that possible at all?

这可能吗?

I've tried something along the lines of this and that didn't work either.

我已经尝试过类似的方法,但也没有奏效。

system("ruby test.rb")
system("echo $?")

or

或者

`ruby test.rb`
exit_code = `echo $?`
if exit_code != 0
  raise 'Exit code is not zero'
end

回答by Stefan

From the documentation:

文档

system returns trueif the command gives zero exit status, falsefor non zero exit status. Returns nilif command execution fails.

true如果命令给出零退出状态,则系统返回,false对于非零退出状态。nil如果命令执行失败则返回。

system("unknown command")     #=> nil
system("echo foo")            #=> true
system("echo foo | grep bar") #=> false

Furthermore

此外

An error status is available in $?.

中提供了错误状态$?

system("VBoxManage createvm --invalid-option")

$?             #=> #<Process::Status: pid 9926 exit 2>
$?.exitstatus  #=> 2

回答by Houcheng

For me, I preferred use `` to call the shell commands and check $? to get process status. The $? is a process status object, you can get the command's process information from this object, including: status code, execution status, pid, etc.

对我来说,我更喜欢使用 `` 来调用 shell 命令并检查 $? 获取进程状态。美元?是一个进程状态对象,可以从这个对象中获取命令的进程信息,包括:状态码、执行状态、pid等。

Some useful methods of the $? object:

$? 的一些有用方法?目的:

   $?.exitstatus => return error code    
   $?.success? => return true if error code is 0, otherwise false
   $?.pid => created process pid

回答by Neil Slater

systemreturns falseif the command has an non-zero exit code, or nilif there is no command.

system返回false,如果命令具有非零退出代码,或者nil如果没有命令。

Therefore

所以

system( "foo" ) or exit

or

或者

system( "foo" ) or raise "Something went wrong with foo"

should work, and are reasonably concise.

应该工作,并且相当简洁。

回答by tadman

You're not capturing the result of your systemcall, which is where the result code is returned:

您没有捕获system调用的结果,这是返回结果代码的位置:

exit_code = system("ruby test.rb")

Remember each systemcall or equivalent, which includes the backtick-method, spawns a new shell, so it's not possible to capture the result of a previous shell's environment. In this case exit_codeis trueif everything worked out, nilotherwise.

请记住,每个system调用或等效方法(包括反引号方法)都会生成一个新的 shell,因此无法捕获先前 shell 环境的结果。在这种情况下exit_codetrue如果一切顺利,nil否则。

The popen3command provides more low-level detail.

popen3命令提供了更多的低级细节。

回答by the Tin Man

One way to do this is to chain them using andor &&:

一种方法是使用andor链接它们&&

system("VBoxManage createvm --name test1") and system("ruby test.rb")

The second call won't be run if the first fails.

如果第一个调用失败,则不会运行第二个调用。

You can wrap those in an if ()to give you some flow-control:

您可以将它们包装在 an 中if ()以提供一些流量控制:

if (
  system("VBoxManage createvm --name test1") && 
  system("ruby test.rb")
) 
  # do something
else
  # do something with $?
end