Ruby: Kernel # `Permission denied to run a command

In an attempt to learn about Ruby execution methods, I found this blog post on five ways to run commands in Ruby http://mentalized.net/journal/2010/03/08/5_ways_to_run_commands_from_ruby/

The author creates an err.rb file that prints two lines: one on STDOUT, the other on STDERR

#!/usr/bin/env ruby puts "out" STDERR.puts "error" 

The first way he tries to launch it is with Kernal # `(backticks)

  >> `./err.rb` err => "out\n" 

The point that illustrates this is that when you start with reverse loops

 STDERR is output, but not captured STDOUT is captured 

When I tried to duplicate this on my system, I got a permission error

 localhost:sites mike$ `./err.rb` -bash: ./err.rb: Permission denied 

I tried sudo ./err.rb unchanged.

I can run ruby err.rb , but this gives a different result than the one received by the author. Namely, by running ruby ​​err.rb, I get

  out error 

Can someone explain why I get a permission error while trying to run it with reverse windows, and also, as a rule, the goal is to run it with reverse steps.

+4
source share
2 answers

Try to add execution permission, use "chmod + x that_file_name" to execute using the format "./file" "file" must have permission to execute

It starts when you ruby, because ruby ​​has execute permission, and it just reads this file.

try it

 localhost:sites mike$ chmod +x err.rb 
+5
source

You need to make an executable file that you can make from your shell:

 chmod +x err.rb 

Then you can call it from the shell as

 ./err.rb 

or from IRB as

 `./err.rb` 

You can also run ruby on it:

 `ruby err.rb` 

Here is a good resource for different reverse loops, system and exec methods.

0
source

Source: https://habr.com/ru/post/1442582/


All Articles