我正在编写一个 ruby 脚本,发现了这种奇怪的行为。
使用 ruby 2.4.2 [x86_64-darwin16]
基本上,我试图回显两条分开的消息,在我的 index.rb
文件中,我得到了:
exec("echo 'teste'")
exec("echo 'teste2'")
但是当我运行 ruby ./index.rb
输出是:
teste
为什么会这样?
这不应该是输出吗?
testeteste2
最佳答案
exec([env,] command... [,options])
Replaces the current process by running the given external command docs
这意味着第一次调用 exec
时用 echo
替换了你的 ruby 程序,所以 ruby 程序的其余部分不会被执行。
您可以使用反引号来运行您想要的命令:
`echo 'teste'`
`echo 'teste2'`
关于Ruby 只执行第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57712673/