I am trying to add functionality to an interesting script. The part
that is tripping me up is that I have to make a system call to echo, and
feed several parameters. One of the parameters occasionally includes
the ` character.
is breaking when #{someVar} contains the tick . This causes the command to fail because it executes up to the tick in someVar and not
to the end as intended.
I have also tried using the system() command as follows:
is breaking when #{someVar} contains the tick . This causes the command to fail because it executes up to the tick in someVar and not
to the end as intended.
I have also tried using the system() command as follows:
But this fails when there is a tick ` in #{someVar} as well.
Here’s a pure Ruby solution:
File.open outputFile, “w” do |out|
IO.popen “./some-script.py”, “rw” do |io|
t = Thread.new do
IO.copy_stream(io, out, 1024)
# pre 1.9:
# while ( b = io.read(1024) )
# out.write(b)
# end
end
io.puts(someVar)
io.close_write
t.join
end
end
Of course, it would be even better if you rewrote the Python script in
Ruby.
On Thu, Feb 10, 2011 at 11:21 AM, Tanaka A. [email protected] wrote:
0000000 f o o ` b a
0000006
Cool! Thanks for sharing.
Is there also a version which would return an IO or accept a block
with IO parameter so we can feed something into the head of the
pipeline and read from the tail?
I am trying to add functionality to an interesting script. The part
that is tripping me up is that I have to make a system call to echo, and
feed several parameters. One of the parameters occasionally includes
the ` character.
Is there also a version which would return an IO or accept a block
with IO parameter so we can feed something into the head of the
pipeline and read from the tail?
% ruby -ropen3 -e ’
Open3.pipeline_w(%w[head -6c], %w[cat -n]) {|w|
w.write “ab\ncd\nef\n”
}’
1 ab
2 cd
This indeed is a good representation of the problem.
I will have to research if my #{someVar} could possibly contain a ’
character. Perhaps Shellwords would be a safer bet.
In case you are still interested, an example of the command with actual
data is below (notice how the sequence @@@@@@S` in the string before " |
collab-table.py" is causing the issue):
echo "Facs Rep|21|@@@@@@Ir||FACS Version 2.1.36.0|2010-09-27 10:57:04.7108-07|21|2|Under Review|||||f||||||f|2010-09-27 11:14:42.686225-07|09/27/2010||||message|t|Facs Rep|09/27/2010|closed|f||18| |||| Facs Rep|21|@@@@@@Lz||FACS Version 3.1.0.0|2010-10-27 10:02:43.778138-07|21|2|Under Review|||||f||||||f|2010-10-27 10:51:17.216686-07|10/27/2010||||message|t|Facs Rep|10/27/2010|closed|f||19| |||| Facs Rep|21|@@@@@@N]||FACS Version 3.2.0.0|2010-11-09 16:56:17.214108-08|21|1|Under Review|||||f||||||f|2010-11-09 16:56:17.214108-08|11/09/2010||||message|t|Facs Rep|11/09/2010|open|f||20| |||| Facs Rep|21|@@@@@@QT||FACS Version 3.3.0.0|2010-12-09 10:19:06.322005-08|21|1|Under Review|||||f||||||f|2010-12-09 10:19:06.322005-08|12/09/2010||||message|t|Facs Rep|12/09/2010|open|f||21| |||| Facs Rep|21|@@@@@@S||FACS Version 3.4.0.0|2011-01-07
10:17:25.142238-08|21|1|Under Review|||||f||||||f|2011-01-07
10:17:25.142238-08|01/07/2011||||message|t|Facs
Rep|01/07/2011|open|f||22| ||||" | ./collab-table.py “message” “D260888”
“hub” “ps_nysdot” “New York State Department of Transportation”
“Alexander Hamilton Bridge & Highbridge Interchange Ramps Rehab” “Facs
Rep” “21” “2011-02-10 11:58:12-0800” “20” “4” “4”>
“cor_6/2011-02-10/D260888/correspondence/CollabmessageList_4.html”`
Open3.pipeline_rw([“cat”, “-n”], [“sed”, “-e”, ‘s/^.*$/<&>/’]) do
|sin, sout, threads|
th = Thread.new(sout) do |io|
io.each_line do |l|
puts l
end
end
5.times do |i|
sin.printf “%03d\n”, i
end
sin.close
th.join
end
13:09:58 Temp$
Cheers
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.