# Interactive System calls within Ruby Script

**URL:** <https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122>\
**Category:** Ruby\
**Created:** [September 20, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122 "2007-09-20T20:11:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [September 20, 2007, 8:11pm UTC](https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122/1 "2007-09-20T20:11:16Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [September 20, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122/2 "2007-09-20T20:22:18Z")

</div>

ZyLo wrote:

> what the call, Ruby will always return to the same directory on the

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.

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [September 20, 2007, 8:24pm UTC](https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122/3 "2007-09-20T20:24:18Z")

</div>

ZyLo wrote:

> what the call, Ruby will always return to the same directory on the

use open4 gem

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [September 20, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/interactive-system-calls-within-ruby-script/115122/4 "2007-09-20T20:33:20Z")

</div>

Zylo:

The problem is the the command pass to the system call is executed in a  
sub-shell, or child process. Any changes made in that sub-shell’s  
environment is _not_ reflected back to the parent. So changing the  
directory in the sub-shell does not affect the parent’s current working  
directory.

When you do two system calls, they are executed in two separate  
sub-shells,  
neither of which affect the other or the parent.

I normally change the directory in the parent before calling system or  
%x{}  
etc:

```
here = Dir.pwd
Dir.chdir 'your-directory here'
begin
    puts "You are now here: #{Dir.pwd}"
    system "your command"
rescue Errno::ENOENT => e
    Dir.chdir here
end

```

Hope that helps a bit.
