How to invoke an external program and get anything the pgm returnsback

V

Victor Reyes

[Note: parts of this message were removed to make it a legal post.]

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
 
J

Jim Menard

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 $?
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

Jim
 
S

Sandor Szücs

I tried:
Ruby script segment:
rc =3D `/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 =20
*false*.


irb> res =3D `ls file_not_exists`
ls: file_not_exists: No such file or directory
=3D> ""
irb> $?.exitstatus
=3D> 1

According to ri page Kernel.system also sets $?


$ qri Kernel.system
---------------------------------------------------------- Kernel#system
system(cmd [, arg, ...]) =3D> 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=FCcs
--=
 
V

Victor Reyes

Ruby script segment:
rc =3D `/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 =3D `ls file_not_exists`
ls: file_not_exists: No such file or directory
=3D> ""
irb> $?.exitstatus
=3D> 1

According to ri page Kernel.system also sets $?


$ qri Kernel.system
---------------------------------------------------------- Kernel#system
system(cmd [, arg, ...]) =3D> 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=FCcs


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"
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
/usr/local/bin>cat test.rb
#!/usr/local/ruby-1.8.6/bin/ruby -W0
puts "This is a Ruby script"
exit 199
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
/usr/local/bin>cat test.ksh
#!/bin/ksh
echo "This is a Korn Shell script"
exit 99
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Thank you

Victor
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top