Forum: Ruby Re: Calling Shell Scripts from Ruby ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gennady B. (Guest)
on 2006-05-03 20:59
(Received via mailing list)
>
> 1)Is it possible to call "Shell Scripts" from ruby? Say if i
> have a shell sciprt named "test.sh", how can i call this from
> ruby? Also if i can call like that,is it possible to get the
> output of that script passed back to ruby?

Shell scripts fall into "any Unix command" category, so keep on reading.

>
> 2)How can i call any "Unix command" residing from a ruby program??

1. Backquotes

result = `test.sh`

2. exec(), fork/exec

exec 'test.sh'
===
Process.wait fork {
  exec 'test.sh'
}

3. popen(), popen3(), etc.

IO.popen('test.sh') { |_io|
  _io.readlines()
}

4. Good old system()

system('test.sh')

Read about all those facilities in the documentation to get the most out
of it.

Gennady.
This topic is locked and can not be replied to.