使用 Rake 进行 Shell 脚本排序
我正在开发一个 rake 实用程序,并希望实现下面提到的功能:
我的 Rake 文件中有一些按顺序排列的 shell 命令。我想要的是该序列应该等待上一个命令完成处理,然后再移动到下一个命令。
sh "git commit -m \"#{args.commit_message}\"" do |ok, res|
# Do some processing
end
sh "git push heroku master"
因此,在上面的示例中,我想要的是
sh "git push heroku master"
之前不应执行
sh "git commit -m \"#{args.commit_message}\"" do |ok, res|
# Do some processing
end
在处理完成
。另外一个好处是,如果我可以将 shell 命令的输出存储在 Ruby 变量中,以便在需要时可以将其用于进一步的操作。
期待尽快收到社区成员的回复。
提前致谢。
I am working on a rake utility and want to implement something mentioned below:
There are some shell commands in a sequence in my Rake file. What I want is that the sequence should wait for the previous command to finish processing before it moves to the next one.
sh "git commit -m \"#{args.commit_message}\"" do |ok, res|
# Do some processing
end
sh "git push heroku master"
So, in the above example what I want is that
sh "git push heroku master"
shouldn't be executed until the processing in the
sh "git commit -m \"#{args.commit_message}\"" do |ok, res|
# Do some processing
end
is completed.
Also another nice to have would be that if I can store the output of the shell command in a Ruby variable so it can be used in further manipulation if required.
Looking forward to a reply from the fellow community member shortly.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除非我遗漏了什么,否则您可以使用 Ruby 的内置命令之一来执行系统命令;查看 http://blog。 jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html 了解更多信息。
该链接中没有提到,但我可能会选择使用反引号(我不确定它是否与系统方法有什么不同)来执行 shell 命令,例如:
...因此,我不明白为什么你不能这样做:
Unless I'm missing something, you can use one of Ruby's built-in commands for executing system commands; take a look at http://blog.jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html for more info.
It's not mentioned in that link, but I'd probably opt for using backticks (which I'm not sure if it's any different than the system method) to execute the shell command like:
...thus, I don't see why you couldn't do: