As a toy problem, I would like to run these commands from within ruby:
$ echo foo
foo
and this variant which used process substitution:
$ cat <(echo foo)
foo
The first form works within ruby using the Kernel#system method:
$ ruby -e 'Kernel.system("echo foo") '
The second does not:
$ ruby -e 'Kernel.system("cat <(echo foo)")'
sh: Syntax error: "(" unexpected
^
This is because instead of /bin/bash Kernel#system calls /bin/sh,
which does not know how to do process substitution with <().
From googling there doesn't seem to be an option to Kernel#system[1]
to specify which shell to use. Is this a limitation of using ruby
1.8.6? Any thoughts to a potential workaround?
[1] http://ruby-doc.org/core/classes/Kernel.html#M005971
Regards,
- Robert
Found a workaround:
$ ruby -e 'cmd="cat <(echo foo)" ;Kernel.system("bash -c \"#{cmd}\"") '
foo
Woks with IO.popen, pipes, and options, too:
$ ruby -e '
cmd=<<-eof.gsub(/\^s+/,"")
cat <(seq 1 20) |
head -3
eof
bash=<<-eof.gsub(/\^s+/,"")
bash -c "#{cmd}"
eof
IO.popen(bash).each {|x| puts x}
'
1
2
3
Regards,
- Robert
3
Regards,
- Robert
--
You received this message because you are subscribed to the Google Groups "Saint Louis Ruby Users Group" group.
To post to this group, send email to stl...@googlegroups.com.
To unsubscribe from this group, send email to stlruby+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/stlruby?hl=en.
$ ruby -e '
> module Kernel
> def self.system(*args)
> args.each do |cmd|
> super "bash -c #{cmd}"
> end
> end
> end
>
> system("cat <(echo $(date))")
>
> '
sh: Syntax error: "(" unexpected
This came almost there:
$ ruby -e '
> module Kernel
> def self.bash(*args)
> args.each do |cmd|
> system("bash -c \"#{cmd}\"")
> end
> end
> end
>
> Kernel.bash("cat <(echo $(date))")
>
> '
Fri Dec 25 12:27:45 EST 2009
BTW, happy holidays to all.
Regards,
- Robert
-Mike