ruby - Ruby 只执行第一行?
问题描述
我正在编写一个 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])
通过运行给定的外部命令docs替换当前进程
这意味着第一次调用 用exec
替换您的 ruby 程序echo
,因此不会执行 ruby 程序的其余部分。
您可以使用反引号来运行您想要的命令:
`echo 'teste'`
`echo 'teste2'`
推荐阅读
- getorgchart - JSON 使用 getorgchart 创建树
- angular - ng2-ckeditor 工具栏无法正常工作
- symfony - 在请求期间检索实体而不是持久化它
- database - 保存在数据库中时所有时区的 Laravel 时间延迟
- rxjs - 切换到新的 observable,将上一个 observable 中的最新作为输入
- javascript - MediaElement.buffered IndexSizeError
- c# - 运行 CMD.exe 或 netsh.exe 的区别
- ssis - 如何使用 SQL Server 身份验证使用目录进行 SSIS 包部署?
- javascript - 在浏览器中显示 .raw 文件图像
- r - 将数据从 R 加载到 PosgreSQL 数据库而不丢失约束