tags:

views:

482

answers:

7

I am new to programming and am trying to follow an example which uses #! comment in ruby.

I am consistently get the message: bash: matz.rb: command not found

I am using this comment: #! /usr/bin/env ruby

I have tried it with and without the space after ! as well as with and without the env.

when I use the $ which ruby

ruby is in: /usr/bin/ruby

I have also gone into the operating system and changed the permissions on the file matz.rb to rwx for all users w/ no effect. Am I doing something wrong or do I have my system set up incorrectly?

+1  A: 

Have you tried the ShaBang as following to directly point to ruby?

#! /usr/bin/ruby

Then you call the script from the commandline as

./matz.rb

Under Unix/Linux systems the dot in front of a command to search for the command in the current directory. If you give a path like /usr/bin/ruby, it will search in the current directory for a directory called usr...

A command without a dot/ in front is searched in locations specified by the path variable of the environment.

A command with a / on the beginning is searched exactly from root following the specified path.

Inside your ShaBang, you want to specify the exact path to the interpreter so "/usr/bin/ruby" is the correct one. On the commandline, where you want your script to be executed, you need to call the script with "./matz.rb" otherwise the bash will search a command like /usr/bin/matz.rb what leads to your errormessage.

BeowulfOF
+6  A: 

If you're in the same directory as the matz.rb file, be sure to run it as

$ ./matz.rb

and not just

$ matz.rb

Here's a shell session demonstrating this working:

$ ls -la m*
-rwxr-xr-x  1 gareth  gareth  32  8 Jan 08:46 matz.rb
$ cat matz.rb
#!/usr/bin/env ruby
puts "Matz"
$ matz.rb
-bash: matz.rb: command not found
$ ./matz.rb
Matz
Gareth
+8  A: 

The /usr/bin/env part is fine. You need to give bash the path to matz.rb when you run it. If you're in the directory with matz.rb, type "./matz.rb". The directory "." means the current directory - bash doesn't look there by default when running programs (like Windows does).

The env program (/usr/bin/env) searches the executable search path - the PATH environment variable - for the ruby program as if you typed it at the command prompt, and runs that program. The shebang comment doesn't do this. If you want to give your script to other people who might not have ruby installed in the same place as you, then you should use the "#!/usr/bin/env ruby" comment so that it will work as long as they can run ruby by typing "ruby".

Doug
+2  A: 

It sounds like you're on a Unix/Linux system and just typing matz.rb on the command line. If you're trying to execute a command in the current directory, you need to call it like ./matz.rb. The "./" tells it to look in the current directory rather than just /usr/bin and friends.

Chuck
+2  A: 

Your file wasn't created on Windows was it? If it has \r\n line endings, that will upset bash. You can open it with Vim and check:

vi matz.rb
:set ff=unix
:wq

If when you tab-complete the "ff=" part it says dos, then it has the wrong file format. Alternatively, run dos2unix and try to run the file again:

apt-get install sysutils
dos2unix matz.rb
rq
A: 

I failed to see any answer indicating you to change the executable mode of the file, so you might wanna try and do

chmod +x matz.rb

before you go and try doing

./matz.rb

Also it might be better not to attach a .rb extension to the file, such is the case for normal ruby / rails scripts e.g. script/generate, script/console etc.

ucron
A: 

You can use the 'shebang' line with either:

#!/usr/bin/ruby
#!/usr/bin/env ruby

But the script needs to be executable (you indicated it is) and in your shell $PATH.

echo $PATH

Put the script in one of those directories, or modify your path, otherwise specify the full path to it, for example:

export PATH=$PATH:/home/user/bin

or one of these:

./matz.rb
/home/user/bin/matz.rb

You can also run the Ruby interpreter passing the script filename as an argument, and it will be executed. This is particularly useful if you have another version of Ruby installed on your system (say, for testing, like Ruby Enterprise Edition, REE):

/usr/bin/ruby matz.rb
/opt/ree/bin/ruby matz.rb
jtimberman