views:

145

answers:

1

Hi All,

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.

A: 

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
turboladen