# How to invoke an external program and get anything the pgm returns back

**URL:** <https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686>\
**Category:** Ruby\
**Created:** [July 21, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686 "2008-07-21T20:41:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 21, 2008, 8:41pm UTC](https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686/1 "2008-07-21T20:41:26Z")

</div>

Team,

I have a Ruby script which might invoke an external program.  
The external program could be written in: Ruby, Perl, KSH, CSH, BSH,  
Etc.  
The invoked program might return a value in the the form of a return  
code.  
For example, a KSH script might execute an _exit 99_ stmt and which  
value I  
would like to capture in the invoker, Ruby script.

I tried:  
Ruby script segment:  
rc = `/usr/local/bin/script_name`

I also tried in the Ruby caller script:  
system("/usr/local/bin/script\_name")

KSH:  
exit 99

# But _rc_ does not have anything of value. It has either _true_ or _false_.

Also, using Ruby to Ruby I still can’t get a Ruby script called another  
Ruby  
script, have the called script returns a value and have the invoker Ruby  
script get the value.

Is there anyway to get what I described above?

Thank you

Victor

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 21, 2008, 9:19pm UTC](https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686/2 "2008-07-21T21:19:34Z")

</div>

On Mon, Jul 21, 2008 at 2:35 PM, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Team,
> 
> I have a Ruby script which might invoke an external program.  
> The external program could be written in: Ruby, Perl, KSH, CSH, BSH, Etc.  
> The invoked program might return a value in the the form of a return code.  
> For example, a KSH script might execute an _exit 99_ stmt and which value I  
> would like to capture in the invoker, Ruby script.

The global variable $? holds the exit status. If you require ‘English’  
then you can use the name $CHILD\_STATUS instead of $?

> KSH:
> 
> Thank you
> 
> Victor

Jim

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 21, 2008, 9:30pm UTC](https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686/3 "2008-07-21T21:30:42Z")

</div>

On 21.07.2008, at 20:35, Victor R. wrote:

> I tried:  
> Ruby script segment:  
> rc = `/usr/local/bin/script_name`
> 
> I also tried in the Ruby caller script:  
> system("/usr/local/bin/script\_name")
> 
> KSH:  
> exit 99

Ask $? to get the information.  
$?.exitstatus

> But _rc_ does not have anything of value. It has either _true_ or  
> _false_.

irb\> res = `ls file_not_exists`  
ls: file\_not\_exists: No such file or directory  
=\> “”  
irb\> $?.exitstatus  
=\> 1

According to ri page Kernel.system also sets $?

## $ qri Kernel.system ---------------------------------------------------------- Kernel#system system(cmd [, arg, …]) =\> true or false

```
  Executes cmd in a subshell, returning true if the command was
  found and ran successfully, false otherwise. An error status is
  available in $?. The arguments are processed in the same way as
  for Kernel::exec.

```

hth. regards, Sandor  
Sz

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 21, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/how-to-invoke-an-external-program-and-get-anything-the-pgm-returns-back/143686/4 "2008-07-21T21:50:42Z")

</div>

On Mon, Jul 21, 2008 at 3:24 PM, Sandor SzÃ¼cs  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > KSH:  
> > irb\> res = `ls file_not_exists`  
> > system(cmd [, arg, …]) =\> true or false
> 
> * * *
> 
> ```
> Executes cmd in a subshell, returning true if the command was
> found and ran successfully, false otherwise. An error status is
> available in $?. The arguments are processed in the same way as
> for Kernel::exec.
> 
> ```
> 
> ## hth. regards, Sandor SzÃ¼cs

Great! It works!  
Here is what I have:

# /tmp\>cat pg1 #!/usr/local/ruby-1.8.6/bin/ruby -W0 puts “Calling /usr/local/bin/test.rb” system(“/usr/local/bin/test.rb”) puts $?.exitstatus puts “Calling /usr/local/bin/test.ksh” system(“/usr/local/bin/test.ksh”) puts $?.exitstatus puts “Now exiting”

# /usr/local/bin\>cat test.rb #!/usr/local/ruby-1.8.6/bin/ruby -W0 puts “This is a Ruby script” exit 199

# /usr/local/bin\>cat test.ksh #!/bin/ksh echo “This is a Korn Shell script” exit 99

And this is the result:

# Calling /usr/local/bin/test.rb This is a Ruby script 199 Calling /usr/local/bin/test.ksh This is a Korn Shell script 99 Now exiting

Thank you

Victor
