jasonz
jasonz

Reputation: 1345

ruby execute shell command error

Here is the code do.rb:

    #!/usr/bin/ruby

    def open
      fp = File.open("input")
      yield fp
      fp.close
    end

    open do |fp|
      while line = fp.gets
        puts `du #{line} -h`
        # puts `du -h #{line}` # this works fine
      end
    end

And the input file:

    1.rb
    2.rb

If I run ruby do.rb, it just returns a command not found.
And I have to change -h before #{line}.
I don't why. Thanks.

Upvotes: 1

Views: 164

Answers (1)

falsetru
falsetru

Reputation: 369424

A string returned by IO#gets contains line separator.

You should remove the line separator using String#chomp:

puts `du #{line.chomp} -h`

Upvotes: 2

Related Questions