Another Newbie question

L

LenS

I am running Ruby 1.8.2 on a XP machine. I have several programs
(executables) which convert text files to a special file format. I
would like to have these programs run from within a ruby script and
check the return code.

In general how would you call the foo.exe program in ruby and check
return code.

Thanks
Len Sumnler
 
J

James Edward Gray II

I am running Ruby 1.8.2 on a XP machine. I have several programs
(executables) which convert text files to a special file format. I
would like to have these programs run from within a ruby script and
check the return code.

In general how would you call the foo.exe program in ruby and check
return code.

You're looking for the Kernel.system() method.

Hope that helps.

James Edward Gray II
 
A

Ara.T.Howard

I am running Ruby 1.8.2 on a XP machine. I have several programs
(executables) which convert text files to a special file format. I
would like to have these programs run from within a ruby script and
check the return code.

In general how would you call the foo.exe program in ruby and check
return code.

Thanks
Len Sumnler


program, input, output = 'foo.exe', 'input.txt', 'output.txt'

command = "#{ program } < #{ input } > #{ output }"

system command

exit_status = $?.exitstatus

hth.

-a
--
===============================================================================
| email :: ara [dot] t [dot] howard [at] noaa [dot] gov
| phone :: 303.497.6469
| Your life dwells amoung the causes of death
| Like a lamp standing in a strong breeze. --Nagarjuna
===============================================================================
 
B

Brian Schröder

I am running Ruby 1.8.2 on a XP machine. I have several programs
(executables) which convert text files to a special file format. I
would like to have these programs run from within a ruby script and
check the return code.
=20
In general how would you call the foo.exe program in ruby and check
return code.
=20
Thanks
Len Sumnler
=20
=20

def execute(*args)
system(*args)
$?
end

execute("echo 'test' | grep -q test") # =3D> #<Process::Status:
pid=3D21948,exited(0)>
execute("echo 'nothing' | grep -q test") # =3D> #<Process::Status:
pid=3D21951,exited(1)>

Read ri Process::Status for more information on the Process::Status Class.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
D

David Vallner

Brian said:
def execute(*args)
system(*args)
$?
end
=20
As a sworn hater of magic global variables, I'd like to know: Is there=20
any way to do this that doesn't involve the "$?" ? Cheers

David Vallner
 
B

Brian Schröder

Brian Schr=F6der wrote:
=20
As a sworn hater of magic global variables, I'd like to know: Is there
any way to do this that doesn't involve the "$?" ? Cheers
=20
David Vallner
=20
=20
=20

Put the above into a library and use execute ;-).

The $? is a thread-local variable, so there is nothing bad about using
it like I did above. There is no possibility that another command
changes the $? before it is returned by "execute". And if you put it
into a function like I did you'll never again the the dollar sign.

Maybe the above mini-function could even get into facets if it's not
already there. Then you would never ever have seen the dollar sign.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 

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