ruby - 为什么 ruby​​ 代码在我第二次调用 exec 后崩溃/退出?

标签 ruby system-calls

puts "start"
ret1 = exec('pwd')
puts ret1
ret2= exec('hostname')
puts ret2

a = "."
puts a

exec('ls ~')
////code exit from here... not any other output why?
puts a
puts a
puts a

我的这段代码在第二次调用 exec 后退出。这是为什么?

% ruby exec.rb
start
/Users/xxx/code/

这是我运行这段代码时的输出。

最佳答案

Kernel#exec 替换当前运行的进程。一旦执行,代码的剩余部分将不会运行。

puts "start"
ret1 = exec('pwd')  # <---- After this, no more remaining code is executed.
...

如果你想得到命令的输出,使用Kernel#`相反:

puts "start"
ret1 = `pwd`

关于ruby - 为什么 ruby​​ 代码在我第二次调用 exec 后崩溃/退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33194564/

相关文章:

ruby - #<Dir :0x00007fcc34ccf6e8> Did you mean? each_slice 的未定义方法 `each_child'

ruby-on-rails - Rails ActiveRecord - 查询关系的最后记录

ruby - .nil?, .blank? 之间的区别?和.empty?

检查输入文件是否是 C 中的有效文件

C - 在不同的过程中使用不同的管端

c++ - 将 C++ 代码从 Windows 移植到 Unix : systemcalls colliding with name of functions

ruby-on-rails - 像 User.find( :all). each? 这样的代码有什么问题?

ruby - 尝试在 Ubuntu : do I still have Ruby installed? 上卸载并全新安装 Ruby

x86 - NASM 中断 x86 引用?

c - malloc 是否负责内存对齐?