使用 Rake 进行 Shell 脚本排序

发布于 2024-08-27 14:03:38 字数 577 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稍尽春風 2024-09-03 14:03:38

除非我遗漏了什么,否则您可以使用 Ruby 的内置命令之一来执行系统命令;查看 http://blog。 jayfields.com/2006/06/ruby-kernel-system-exec-and-x.html 了解更多信息。

该链接中没有提到,但我可能会选择使用反引号(我不确定它是否与系统方法有什么不同)来执行 shell 命令,例如:

output = `ls`     # => gets the output of the ls command to the output variable

...因此,我不明白为什么你不能这样做:

output = `git commit -m "#{args.commit_message}"` do |ok, res|
  # Do some processing
end

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:

output = `ls`     # => gets the output of the ls command to the output variable

...thus, I don't see why you couldn't do:

output = `git commit -m "#{args.commit_message}"` do |ok, res|
  # Do some processing
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文