tags:

views:

53

answers:

2

This isn't working in ruby and I don't understand why?

#!/usr/bin/env ruby

exec "sort data.txt > data.sort"

data.txt is a comma sepparated file. Anyway.. If I try to execute that line in the shell it works without a problem.

When I try to run this script from my script, I get an empty file.

A: 

Have you tried

%x(sort data.txt > data.sort)
Shreyas Satish
I just tried that it didn't work.. I substitute the exec line with yours right?
Ted Flethuseo
Oh.. I think I now know what's happening... I actually write the file myself in a few lines above it. file = File.new("data.txt", "w") and then it probably has not flushed that to disk.. and so when it actually executes the command there is nothing to sort. How do I flush to disk? – Ted Flethuseo 8 mins ago Ah.. I need to close the file file.close
Ted Flethuseo
A: 

This isn't really an answer, but I wanted to share that your original usage of exec is actually working for me. This was how I set it up.

data.txt

"1,2,3,4,5,6,7,8"

sort.rb (I don't know what your sort did so I am just writing the same data out)

File.open(ARGV[0]){|f| puts f.read}

irb session

irb(main):001:0> exec "sort data.txt > data.sort"

When I ran this in irb, I did get a data.sort output file and it contained "1,2,3,4,5,6,7,8" as expected. I can run the same exec line through irb or from another ruby file, and I get the output file with data each time.

I am running Ruby 1.8.6 on a 32bit Windows XP system.

D-D-Doug
Oh.. I think I know what's happening... I actually write the file myself in a few lines above it. file = File.new("data.txt", "w") and then it probably has not flushed that to disk.. and so when it actually executes the command there is nothing to sort. How do I flush to disk?
Ted Flethuseo
Ah.. I need to close the file file.close
Ted Flethuseo
Thank anyway, I figure out things by discussing them.
Ted Flethuseo