【问题标题】:Running command from Ruby从 Ruby 运行命令
【发布时间】:2015-12-06 05:55:55
【问题描述】:

从 Ruby 执行命令时,我看到异常行为。当我跑步时

`find ./ -type f -exec sed -i -e  's/\/\/static.squarespace.com/http:\/\/static.squarespace.com/g' {} \;`

从终端,它可以工作,但是当我尝试在 Ruby 中使用

做同样的事情时
`system(find ./ -type f -exec sed -i -e 's/\/\/static.squarespace.com/http:\/\/static.squarespace.com/g' {} \;)`

我明白了

find: -exec: no terminating ";" or "+"

我怎样才能在 Ruby 中成功地做到这一点?

【问题讨论】:

  • 您似乎忘记在您的问题中添加一些"s;因此,鉴于您拥有的代码不可执行,我们无法 100% 确定地找到问题。始终尽可能准确地复制粘贴您的问题。但很可能你需要加倍使用你的反斜杠。

标签: ruby shell sed terminal


【解决方案1】:

确保双倍转义反斜杠 - 否则 \; 将变为 ;。这有效:

cmd = "find ./ -type f -exec sed -i -e 's#\/\/static.squarespace.com#http:\/\/static.squarespace.com#g' {} \\;"
system(cmd)

【讨论】:

  • 现在我得到sed: 1: "s///static.squarespace. ...": bad flag in substitute command: 's'
  • 我明白了 - 更新了答案。使用# 作为分隔符而不是/
猜你喜欢
  • 2018-10-27
  • 2010-10-03
  • 2015-08-13
  • 1970-01-01
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
相关资源
最近更新 更多