从Ruby脚本执行Shell脚本

5

我有以下内容:

_shellScript = "../Dependency/test.sh"
exec 'sh #{_shellScript}'

但是当我尝试执行ruby脚本时,却只在shell提示符处停止了,而没有执行存储在变量_shellScript中的脚本。

如果有任何想法,将不胜感激!


4
你的代码在 exec 字符串中使用了单引号。在单引号字符串中,Ruby 不支持插值(#{})。这也可能是错误的原因。 - Linuxios
2个回答

9
在Ruby中,exec结束Ruby进程并将shell传递给指定的子进程。如果您需要将test.sh的所有输出传递到控制台,则需要使用system("sh #{_shellScript}")。如果您需要通过stdin和stdout传递数据并接收数据,请查看popen

我认为你的评论可能更接近实际问题,'sh #{_shellScript}' 中的 # 将被视为注释,因此它与 exec 'sh' 和命令提示符相同。 - mu is too short
你可能同时修复当前的错误(单引号而不是双引号)和下一个错误(exec 而不是 system) :) - mu is too short

1

只需尝试以下行:

%x[#{_shellScript}]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接