Interactive System calls within Ruby Script

Z

ZyLo

I'm not sure whether this is possible or not, but I know that Ruby
allows you to make system calls in many different ways, and I
generally use:

system "echo Hello"

or something when I want to call outside of the script. What I would
like to know is if it is possible to make these system calls
interactive? For instance, I was toying with directory manipulation
using system "cd .." system "pwd", etc., and I noticed that no matter
what the call, Ruby will always return to the same directory on the
system "pwd" command.

I was wondering if it is possible to send multiple commands so you
could be a bit more interactive with it? I know there are libraries
out there to do this for me, but I am looking at this more from the
standpoint that I'm not planning on using this ability for directory
manipulation.

Another thing I was wondering is whether you can get output from the
terminal on system calls. I tried somethign like this:

system "pwd"
gets directorystring
puts directorystring

Only my script doesn't get the input at all, and hangs(expecting an
input I assume).

How can you usefully use system for calling functions if it doesn't
remember state(directory, for instance), or allow you to get back
terminal information?
 
M

Mohit Sindhwani

ZyLo said:
I'm not sure whether this is possible or not, but I know that Ruby
allows you to make system calls in many different ways, and I
generally use:

system "echo Hello"

or something when I want to call outside of the script. What I would
like to know is if it is possible to make these system calls
interactive? For instance, I was toying with directory manipulation
using system "cd .." system "pwd", etc., and I noticed that no matter
what the call, Ruby will always return to the same directory on the
system "pwd" command.

I was wondering if it is possible to send multiple commands so you
could be a bit more interactive with it? I know there are libraries
out there to do this for me, but I am looking at this more from the
standpoint that I'm not planning on using this ability for directory
manipulation.

Another thing I was wondering is whether you can get output from the
terminal on system calls. I tried somethign like this:

system "pwd"
gets directorystring
puts directorystring

Only my script doesn't get the input at all, and hangs(expecting an
input I assume).

How can you usefully use system for calling functions if it doesn't
remember state(directory, for instance), or allow you to get back
terminal information?
I'm sure you'll get back much better answers than mine but just to get
you started, this is what I have been doing. When needing to change
directory within my script, I usually do it from the ruby equivalent.
Also, as for getting back what the command you called sent to its
stdout, use back ticks...

for example,

output_from_cmd = `myotherprogram.exe`

this sends the stdout of the myotherprogram.exe to the variable
output_from_cmd within your script.

Hope this helps some.

Cheers,
Mohit.
9/21/2007 | 2:20 AM.
 
M

Marcin Raczkowski

ZyLo said:
I'm not sure whether this is possible or not, but I know that Ruby
allows you to make system calls in many different ways, and I
generally use:

system "echo Hello"

or something when I want to call outside of the script. What I would
like to know is if it is possible to make these system calls
interactive? For instance, I was toying with directory manipulation
using system "cd .." system "pwd", etc., and I noticed that no matter
what the call, Ruby will always return to the same directory on the
system "pwd" command.

I was wondering if it is possible to send multiple commands so you
could be a bit more interactive with it? I know there are libraries
out there to do this for me, but I am looking at this more from the
standpoint that I'm not planning on using this ability for directory
manipulation.

Another thing I was wondering is whether you can get output from the
terminal on system calls. I tried somethign like this:

system "pwd"
gets directorystring
puts directorystring

Only my script doesn't get the input at all, and hangs(expecting an
input I assume).

How can you usefully use system for calling functions if it doesn't
remember state(directory, for instance), or allow you to get back
terminal information?
use open4 gem
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top