[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

I'm a process-spawning idiot.

Devin Mullins

11/29/2006 7:00:00 AM

I'm looking for a method that, like Kernel#` and IO::popen, allows me to
call out to the system and grab its stdout. However, I'd like it to
accept multiple args (like Kernel#system), so I don't have to worry
about filenames with spaces and backslashes and the like. Alternatively,
I'll take a lib that escapes strings for Kernel#`, provided it works on
win32, unix, cygwin...

I'm sure I'm just missing the obvious. Help?

Thanks,
Devin

7 Answers

Paul Lutus

11/29/2006 7:48:00 AM

0

Devin Mullins wrote:

> I'm looking for a method that, like Kernel#` and IO::popen, allows me to
> call out to the system and grab its stdout. However, I'd like it to
> accept multiple args (like Kernel#system), so I don't have to worry
> about filenames with spaces and backslashes and the like. Alternatively,
> I'll take a lib that escapes strings for Kernel#`, provided it works on
> win32, unix, cygwin...
>
> I'm sure I'm just missing the obvious. Help?

You want to what? You want to execute an arbitrary system command and
collect the command's output?

data = `command arg1 arg2 arg3`

Obviously if the provided command line works in a shell, it will work here,
and vice versa.

--
Paul Lutus
http://www.ara...

Eric Hodel

11/29/2006 7:58:00 AM

0

On Nov 28, 2006, at 2350 , Paul Lutus wrote:

> Devin Mullins wrote:
>
>> I'm looking for a method that, like Kernel#` and IO::popen, allows
>> me to
>> call out to the system and grab its stdout. However, I'd like it to
>> accept multiple args (like Kernel#system), so I don't have to worry
>> about filenames with spaces and backslashes and the like.
>> Alternatively,
>> I'll take a lib that escapes strings for Kernel#`, provided it
>> works on
>> win32, unix, cygwin...
>>
>> I'm sure I'm just missing the obvious. Help?
>
> You want to what? You want to execute an arbitrary system command and
> collect the command's output?
>
> data = `command arg1 arg2 arg3`
>
> Obviously if the provided command line works in a shell, it will
> work here,
> and vice versa.

system 'command', 'arg with spaces' will execute correctly (ARGV[0]
will be 'arg with spaces')

`command arg with spaces` requires manual intervention (ARGV[0] will
be 'arg').

That said, try playing with shell.rb. I don't really know what it
does, though.

--
Eric Hodel - drbrain@segment7.net - http://blog.se...

I LIT YOUR GEM ON FIRE!


Devin Mullins

11/29/2006 1:58:00 PM

0

Eric Hodel wrote:
> That said, try playing with shell.rb. I don't really know what it
> does, though.
Thanks!

This Ruby program:
STDOUT.reopen open('log.txt','w')
exec 'echo', 'Hi!'
__END__
outputs "Hi!" to log.txt.

shell.rb (or, more specifically, shell/process-controller.rb), uses this
combined with fork (since exec replaces the process) to do its dirty work.

Downloading win32-process now... :D

Devin

Devin Mullins

11/29/2006 2:40:00 PM

0

For reference's sake, I don't need fork. The same trick works with
Kernel#system. Some ugly code:
inp, out = IO.pipe
# start Thread.critical
old_stdout = STDOUT.dup
STDOUT.reopen out
system 'echo', 'matz is nice!'
STDOUT.reopen old_stdout
# end Thread.critical
out.close
puts "Hey guess what: #{inp.read}"
__END__

Out of curiosity, is this one of those places where I'd _have_ to use
Thread.critical (assuming I don't want other threads putsing to my pipe)?

Devin

Devin Mullins wrote:
> This Ruby program:
> STDOUT.reopen open('log.txt','w')
> exec 'echo', 'Hi!'
> __END__
> outputs "Hi!" to log.txt.
>
> shell.rb (or, more specifically, shell/process-controller.rb), uses this
> combined with fork (since exec replaces the process) to do its dirty work.
>
> Downloading win32-process now... :D

Ara.T.Howard

11/29/2006 2:45:00 PM

0

Ara.T.Howard

11/29/2006 2:53:00 PM

0

Devin Mullins

11/30/2006 1:50:00 AM

0

ara.t.howard@noaa.gov wrote:
> On Wed, 29 Nov 2006, Devin Mullins wrote:
> <snip>
> this is an insanely bad idea: a cross platform way to hang you system.
well, then. no need to be curt about it. :P

> gem install systemu
aha! i had a stinking suspicion i should just've emailed you, and not
all of ruby-talk. thanks for the info, and the cool lib, ara.

devin
(tmp_dir? Marshal.dump? Yikes. Glad I didn't have to write that crap.
Quick, someone put in an RCR for Kernel#backtick(cmd, *args) so this
craziness can go away.)