Emily Kingan
Emily Kingan

Reputation: 303

Permission Denied when passing files to a ruby program

I have a ruby program that accepts files as input. I am trying to test that this functionality works by piping a file into the program by entering

cat file1.txt | ./app.rb

However, when I do this I get -bash: ./app.rb: Permission denied I have tried using sudo cat file1.txt | ./app.rb which prompts me for my password and then it appears nothing happens.

This works fine when I instead type ruby app.rb file1.txt

Does anyone have any tips for how to get this to work?

As pointed out in the comments, I need to be able to read a file path from stdin AND pass them as parameters:

In my code I have this:

def input
    if ARGV.length.positive?
      ARGV
    else
      gets.chomp.split(' ')
    end
  end

I expect input to return an array of file paths.

Upvotes: 1

Views: 1160

Answers (1)

Tom Lord
Tom Lord

Reputation: 28305

As mentioned in the comments above, sending the contents of a file as STDIN to a program and passing the filename as a parameter are two very different things.

I cannot say which, if either, is "right" or "wrong" without knowing more context of what it is you're actually trying to achieve, but it's important to recognise the difference.


Now, the actual cause of the error here is that you're trying to execute the ruby file directly. You can fix this by running ruby on the filename instead:

cat file1.txt | ruby app.rb

It is possible to execute the file without writing ruby, but you must first make it executable:

chmod +x app.rb

And also write a Shebang at the top of the file, to specify that it should be executed as a ruby script, not a bash script (which is the default):

#!/usr/bin/env ruby

Upvotes: 2

Related Questions