2

script.sh

echo First!
sleep 5
echo Second!
sleep 5
echo Third!

another_script.rb

%x[./script.sh]

I want another_script.rb to print the output of script.sh as it happens. That means printing "First!", waiting five seconds, printing "Second!', waiting 5 seconds, and so on.

I've read through the different ways to run an external script in Ruby, but none seem to do this. How can I fulfill my requirements?

1 Answer 1

1

You can always execute this in Ruby:

system("sh", "script.sh")

Note it's important to specify how to execute this unless you have a proper #!/bin/sh header as well as the execute bit enabled.

Sign up to request clarification or add additional context in comments.

4 Comments

The script has a proper shebang. system "./script.sh" produces the same results as %s[./script.sh]. I'm looking to print the script's output in real time as it happens, not get the script's output as the return value.
For me, it runs your script and shows the output correctly. Do you mean to capture the script's output and process it with Ruby? If so, you need to use popen3.
Hmm, what version of Ruby are you using?
Ah, got it working. Something was up with my RVM setup. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.