简体   繁体   English

在 IT 块 ruby 内的 unix 中的 sed 命令中传递动态值

[英]pass dynamic value in the sed command in unix inside IT block ruby

output.txt: output.txt:

select * from table startTime.... (very big query)

abc.rb: abc.rb:

it "example" do
time = 6537290102
replacestarttime = `sed -i -e 's/startTime/$time/g' ./db/output.txt`
end

expected output---- output.txt:预期输出---- output.txt:

select * from table 6537290102....

In output.txt, the value should be 6537290102 please someone help.在 output.txt 中,值应该是 6537290102 请帮助。 thanks in advance提前致谢

If I hardcode the time value as shown below in sed command then it is working fine whereas the passing the dynamic value "time" is not working replacestarttime = sed -i -e 's/startTime/6537290102/g'./db/output.txt如果我在 sed 命令中硬编码时间值,那么它工作正常,而传递动态值“时间”不起作用 replacestarttime = sed -i -e 's/startTime/6537290102/g'./db/output.txt

Interpolate Your String in Ruby在 Ruby 中插入您的字符串

Right now, you're expecting sed to access a shell variable named $time .现在,您期望 sed 访问名为$time的 shell 变量。 This variable doesn't exist, and wouldn't work as-is even if it did.这个变量不存在,即使它存在也不会按原样工作。

To fix this, you should to interpolate your Ruby variable into the string passed to sed. You can do this as follows:要解决此问题,您应该将Ruby变量插入到传递给 sed 的字符串中。您可以按如下方式执行此操作:

it "example" do
  time = 6537290102
  replacestarttime = `sed -i -e 's/startTime/#{time}/g' ./db/output.txt`
end

Replace single quotes with double quotes用双引号替换单引号

`sed -i -e "s/startTime/$time/g" ./db/output.txt`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM