Another Newbie question

(LenS) #1

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

(James Edward Gray II) #2

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

Hope that helps.

James Edward Gray II

···

On Aug 15, 2005, at 9:06 AM, LenS wrote:

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.

(Brian Schröder) #3

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

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

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

regards,

Brian

···

On 15/08/05, LenS <lsumnler@uniqueinsuranceco.com> wrote:

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

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

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

(Ara.T.Howard) #4

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

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

   system command

   exit_status = $?.exitstatus

hth.

-a

···

On Mon, 15 Aug 2005, LenS wrote:

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

--

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

===============================================================================

(Len Sumnler) #5

Thanks everyone I look at all of your suggestions.

Len Sumnler

(David Vallner) #6

Brian Schröder wrote:

···

On 15/08/05, LenS <lsumnler@uniqueinsuranceco.com> wrote:

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

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

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

David Vallner

(Brian Schröder) #7

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

···

On 30/08/05, David Vallner <david@vallner.net> wrote:

Brian Schröder wrote:

>On 15/08/05, LenS <lsumnler@uniqueinsuranceco.com> wrote:
>
>>In general how would you call the foo.exe program in ruby and check
>>return code.
>>
>>
>def execute(*args)
> system(*args)
> $?
>end
>
>
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

David Vallner

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

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