gpt4 book ai didi

ruby - 运行一个进程,捕获它的输出并退出代码

转载 作者:数据小太阳 更新时间:2023-10-29 08:37:49 24 4
gpt4 key购买 nike

我知道如何运行外部进程,因为有很多方法可以这样做。但是如何将其输出 退出代码捕获到变量中呢?这些最流行的运行流程的方式并不像我想要的那样工作:

a = `ls -l` # "a" captures only output
b = system "ls -l " # "b" captures only exit code

最佳答案

最合适的是阅读$?.exitstatus:

a = `ls -l`        # gets output
b = $?.exitstatus # gets exit code

测试:

`true`
# => ""
$?.exitstatus
# => 0
`false`
# => ""
$?.exitstatus
# => 1
$?.class
# => Process::Status

参见 Process::Status了解更多处理退出状态的方法。

关于ruby - 运行一个进程,捕获它的输出并退出代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25151683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com