Problem with system() in Ruby

B

bbiker

I have written a program that uses a system() call.
I can only run the program once in a dos window.
Each run must be made in a newly opened dos window.

I conducted the following tests to determine if the problem resides in
the program (bigdeal.exe) invoked by the system() or in Ruby.

I wrote two test programs that uses bigdeal.exe in the system() call,
one in ruby and one in perl.

The results are shown below. I believe that the results clearly show
the problem resides in Ruby. It appears that it is NOT a bigdeal.exe or
a DOS problem since the perl program succeeds every time. Perl's
system() does not have this problem.

############## first run
C:\Projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

############## second run
C:projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

NOTE I can run the bigdeal.pl as often as I want in the same DOS window

#########################################################################
# in a new window
#########################################################################

C:\Projects\Ruby\DDA\lib>bigdeal.rb

Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: ""

As shown above the system() call to bigdeal.exe succeeded and the
output
file was produced.

##########################################################################
Any subsequent attempts to issue the command in the same window, the
system()
call fails in the following manner:


system() freezes after bigdeal prints its prompts and processes the
keyboard inputs...
the system() call does not return anything in bigdeal.rb and the output
file is not created.

Big Deal version 1.1

Type random characters until you are told to stop
That is enough

NEED TO KILL THE DOS WINDOW at this point

############################################################################
NOTE The same behavior occurs if I attempt to run bigdeal.rb in the
same window that bigdeal.pl ran.

NOTE I can run bigdeal.pl in the same window I ran bigdeal.rb.


__RUBY_CODE__

#!c:/ruby/bin/ruby -w

# generate 36 random deals
retval = system("bigdeal -n 36 -p rb_deals")

print "system call returned: \"#{retval}\"\n"

__PERL_CODE__

#! perl
use warnings;
use strict;

# generate 36 random deals
my $retval = system ("bigdeal -n 36 -p df_deals");

print "system call returned: \"$retval\"\n";

Hopefully someone more knowledgeable and capable than I will confirm
the problem and resolved it.

bigdeal.exe can be obtained from http://www.xs4all.nl/~sater/

the very first time you run bigdeal.exe, it will ask several questions.
I requested the output file to be in CSV format.
 
R

Robert Klemme

bbiker said:
I have written a program that uses a system() call.
I can only run the program once in a dos window.
Each run must be made in a newly opened dos window.

I conducted the following tests to determine if the problem resides in
the program (bigdeal.exe) invoked by the system() or in Ruby.

I wrote two test programs that uses bigdeal.exe in the system() call,
one in ruby and one in perl.

The results are shown below. I believe that the results clearly show
the problem resides in Ruby. It appears that it is NOT a bigdeal.exe or
a DOS problem since the perl program succeeds every time. Perl's
system() does not have this problem.

############## first run
C:\Projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

############## second run
C:projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

NOTE I can run the bigdeal.pl as often as I want in the same DOS window

#########################################################################
# in a new window
#########################################################################

C:\Projects\Ruby\DDA\lib>bigdeal.rb

Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: ""

As shown above the system() call to bigdeal.exe succeeded and the
output
file was produced.

##########################################################################
Any subsequent attempts to issue the command in the same window, the
system()
call fails in the following manner:


system() freezes after bigdeal prints its prompts and processes the
keyboard inputs...
the system() call does not return anything in bigdeal.rb and the output
file is not created.

Big Deal version 1.1

Type random characters until you are told to stop
That is enough

NEED TO KILL THE DOS WINDOW at this point

############################################################################
NOTE The same behavior occurs if I attempt to run bigdeal.rb in the
same window that bigdeal.pl ran.

NOTE I can run bigdeal.pl in the same window I ran bigdeal.rb.


__RUBY_CODE__

#!c:/ruby/bin/ruby -w

# generate 36 random deals
retval = system("bigdeal -n 36 -p rb_deals")

Try this:
retval = system "cmd", "/c", "bigdeal -n 36 -p rb_deals"

robert
 
R

Robert Klemme

bbiker said:
I have written a program that uses a system() call.
I can only run the program once in a dos window.
Each run must be made in a newly opened dos window.

I conducted the following tests to determine if the problem resides in
the program (bigdeal.exe) invoked by the system() or in Ruby.

I wrote two test programs that uses bigdeal.exe in the system() call,
one in ruby and one in perl.

The results are shown below. I believe that the results clearly show
the problem resides in Ruby. It appears that it is NOT a bigdeal.exe or
a DOS problem since the perl program succeeds every time. Perl's
system() does not have this problem.

############## first run
C:\Projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

############## second run
C:projects\Ruby\DDA\lib>perl bigdeal.pl
Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: "0"

NOTE I can run the bigdeal.pl as often as I want in the same DOS window

#########################################################################
# in a new window
#########################################################################

C:\Projects\Ruby\DDA\lib>bigdeal.rb

Big Deal version 1.1

Type random characters until you are told to stop
That is enough
system call returned: ""

As shown above the system() call to bigdeal.exe succeeded and the
output
file was produced.

##########################################################################
Any subsequent attempts to issue the command in the same window, the
system()
call fails in the following manner:


system() freezes after bigdeal prints its prompts and processes the
keyboard inputs...
the system() call does not return anything in bigdeal.rb and the output
file is not created.

Big Deal version 1.1

Type random characters until you are told to stop
That is enough

NEED TO KILL THE DOS WINDOW at this point

############################################################################
NOTE The same behavior occurs if I attempt to run bigdeal.rb in the
same window that bigdeal.pl ran.

NOTE I can run bigdeal.pl in the same window I ran bigdeal.rb.


__RUBY_CODE__

#!c:/ruby/bin/ruby -w

# generate 36 random deals
retval = system("bigdeal -n 36 -p rb_deals")

print "system call returned: \"#{retval}\"\n"

__PERL_CODE__

#! perl
use warnings;
use strict;

# generate 36 random deals
my $retval = system ("bigdeal -n 36 -p df_deals");

print "system call returned: \"$retval\"\n";

Hopefully someone more knowledgeable and capable than I will confirm
the problem and resolved it.

bigdeal.exe can be obtained from http://www.xs4all.nl/~sater/

the very first time you run bigdeal.exe, it will ask several questions.
I requested the output file to be in CSV format.

Try using system with an array of arguments instead of the single string
to avoid the shell. Does it help?

robert
 
B

bbiker

Thank you Robert.

Your first suggestion works!

I am not sure about your second suggestion.

I tried: retval = system("bigdeal", "-n 36", "-p rb_deals")
and retval = system("bigdeal", "-n 36 -p rb_deals")

both return immediately with retval set to 'false"

Note;
with your suggestion, the retval value is 'true'

Ruby documentation on system() is sparse ... I'll go back to perl's
documentation and try to implement your second suggestion correctly. I
would prefer the second way since it seems more intuitive than the
first.

Once again, thank you for your help.
 
R

Robert Klemme

bbiker said:
Thank you Robert.

Your first suggestion works!

I am not sure about your second suggestion.

I tried: retval = system("bigdeal", "-n 36", "-p rb_deals")
and retval = system("bigdeal", "-n 36 -p rb_deals")

No, that would rather be

retval = system("bigdeal", "-n", "36", "-p", "rb_deals")

You can also do (for less quoting clutter)

retval = system *w{bigdeal -n 36 -p rb_deals}

You need to pass *all* arguments individually. You may have to replace
"bigdeal" with the complete path.

Regards

robert
 
B

bbiker

Robert said:
No, that would rather be

retval = system("bigdeal", "-n", "36", "-p", "rb_deals")

You can also do (for less quoting clutter)

retval = system *w{bigdeal -n 36 -p rb_deals}
having to put absolute path is not practical since the program can be
run from any directory as long as bigdeal.exe resides in that
directory.

I tried to use relative path but I got an error message although the
program ran and produced an output file.

C:/../bigdeal.rb:8:in `system': wrong first argument (ArgumentError)
from C:/Documents and Settings/Owner/My Documents/My
Projects/Ruby/DDA/lib/bigdeal.rb:8

So I will stick to your first suggestion since it works fine. I
believe all the problems arise because bigdeal.exe requires interaction
with the user (invoker) via a console.
 

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,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top