I'm on OS X (with bash) and a newbie at unix. I want to know if it's possible to amend some file such that to run a ruby program, I don't need "ruby file.rb", but instead can just run "ruby.rb".
Is there a reason NOT to do this?
Thanks!
Yes you can do this.
Assuming ruby.rb has something like this in it:
#!/usr/bin/env ruby
puts 'Hello world'
At the command line: chmod +x ruby.rb
This makes it executable.
Then you can execute it like this:
./ruby.rb
For more details see wikibooks.
EDIT (Jörg W Mittag): Using #!/usr/bin/env ruby instead of #!/usr/bin/ruby as the shebang line is more portable, because on every Unix produced in the last 20 years, the env command is known to live in /usr/bin, whereas Ruby installations are typically all over the place. (E.g., mine lives in /home/joerg/jruby-1.2.0/bin/ruby.)
As others have mentioned, you want to have a shebang (#!) line at the beginning, and change the permissions to executable.
I would recommend using #!/usr/bin/env ruby instead of the path to Ruby directly, since it will make your script more portable to systems that may have Ruby installed in different directories; env will search in your search path, and so it will find the same Ruby that you would execute if you ran ruby on the command line. Of course, this will have problems if env is in a different location, but it is much more common for env to be at /usr/bin/env than for Ruby to be at /usr/bin/ruby (it may be in /usr/local/bin/ruby, /opt/bin/ruby, /opt/local/bin/ruby, etc)
#!/usr/bin/env ruby
puts "Hello!"
And make it executable:
chmod +x file.rb
chmod +x /path/to/file
No reason not to do it, as long as you prefix the interpreter with a shebang (#!/usr/local/ruby or whatever the path is on OSX). The shell doesn't care.
Place the correct shebang as the first line of your file. ex:
#!/usr/bin/ruby
in the shell, make the file executable
chmod +x file
If you want to do anything more complicated with running this application, you can always create a shell script:
#! /bin/sh
ruby ruby.rb
If you save it to run_script, you just have to chmod +x it as mentioned previously, then execute the following command:
$ ./run_script
I doubt this will be any more useful in your particular situation than the solutions already mentioned, but it's worth noting for completeness's sake.
Related
I'm pretty inexperienced with terminal type stuff besides the most basic commands, I recently switched from Bash to ZSH with in oh-my-zsh. I'm trying to make an executable ruby script at usr/bin/test.rb. For what it's worth, I never tried this with bash so I have no idea if its zsh specific.
$~ test.rb
/usr/bin/test.rb: line 2: puts: command not found
$~ ruby test.rb
ruby: No such file or directory -- test.rb (LoadError)
and my .zshrc file:
export ZSH=$HOME/.oh-my-zsh
source $ZSH/oh-my-zsh.sh
export PATH="/Users/jason/.rvm/gems/ruby-2.0.0-p247/bin:/Users/jason/.rvm/gems/ruby- 2.0.0-p247#global/bin:/Users/jason/.rvm/rubies/ruby-2.0.0-p247/bin:/Users/jason/.rvm/bin:.git/safe/../../bin:.git/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/Users/jason/.rvm/bin"
.
~ which ruby
/Users/jason/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
test.rb
#!/usr/bin/env ruby
puts 'test!'
$~ test.rb
/usr/bin/test.rb: line 2: puts: command not found
This is probably because your script is missing an essential line, often called a "pound-bang line" or, more simply, a "bang line", which tells the operating system what program to use to execute the rest of the file. Typically, for Ruby scripts, it looks like:
#!/usr/bin/ruby
or
#!/usr/bin/env ruby
and MUST be the first line in the file. When the OS opens the file, it looks for #! and, if it sees those, launches the executable at the path given, and passes the script to it. That's basic script execution on a *nix system, and applies to sh/Bash/Perl/Python/Ruby and any number of other executable applications on a *nix system.
ruby test.rb
ruby: No such file or directory -- test.rb (LoadError)
I suspect the second failed because you weren't in the /usr/bin/ directory when you executed that command. Ruby tried to run the script but couldn't find it in the local/current directory.
I'm not trying to be cruel, but, as a programmer, you'll spend a huge amount of time at the command-line, especially so if you are programming in C/C++, Perl, Ruby, Python, or any non-IDE based language. You have to learn how the OS works otherwise disasters of varying sizes and shapes await you, so, in parallel to learning a language you need to learn how to use, and administer, your OS. You don't have to be a power-user or administrator, but you have to know enough to understand good instructions from ones that don't apply, or are just plain-wrong.
Well I was facing the same problem, I had ruby & rails installed but I couldn't run them on ZSH
The answer is so simple
Just Add the following lines to .zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Then run
source ~/.zshrc
After that to check if ruby & rails are found by ZSH run
ruby --version
rails --verison
I created a shell script to automate some processes, something like that:
#!/bin/bash
ruby RubyFile.rb
But when I run this script, I get this error:
ruby(2882): Operation not permitted
Any one knows what the hell is this?
I use this when I want want to run Ruby code "as executable".
#!/usr/bin/env ruby
And then chmod the script.
chmod +x script.rb
And run it
./script.rb
I suggest usage of env because running a command through /usr/bin/env has the benefit of looking for whatever the default version of the program is in your current environment.
Why so complicated? Why not just
#!/usr/bin/ruby
or wherever your ruby is?
I'm looking for solution how can run Ruby script without invoking it directly like
ruby /path/to/file.rb
So far, I have been using aliases in my .bashrc to create shortcut like
alias myscript='ruby /path/to/file.rb'
But now, I need to create a gem which I'd like to use on different computers and my current approach doesn't fit well for this.
What you could do is the following:
Create a shell script which invokes the Ruby script as your alias does:
ruby /path/to/file.rb
Set a softlink to the /usr/bin/ path to invoke it in the shell using somecommand:
ln -s /full/path/to/the/previously/created/shellscript /usr/bin/somecommand
If you wanna go further, you could create a shell script which does the soft-linking automatically.
Add a shebang to the beginning of the script
#!/usr/bin/env ruby
(check that shebang is #!)
then make your script executable
chmod +x file.rb
Now you can run the file as a "standalone" executable
# For example
$ ./file.rb
("Standalone", because the ruby interpreter still needs to be installed somewhere in your path.)
I'm using Ubuntu. I'm trying to run a ruby file todo.rb
I added this shebang as the very first line of the file
#!/usr/bin/env ruby
I go to the directory where the rb file is located and then run todo.rb and get error todo.rb: command not found.
So I went directly to the /usr/bin directory. I found the env command and ran it. The output of the env command displays ruby paths and ruby data:
MY_RUBY_HOME=/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290
PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global/bin:/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin:/home/tallercreativo/.rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
GEM_PATH=/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290:/home/tallercreativo/.rvm/gems/ruby-1.9.2-p290#global
RUBY_VERSION=ruby-1.9.2-p290
So since, I couldn't make it work, I changed the shebang to point to ruby directly:
#!/home/tallercreativo/.rvm/rubies/ruby-1.9.2-p290/bin/ruby
and I get same command not found error. What's wrong?
You need to first make your script executable:
chmod +x todo.rb
then you need to run it like so:
./todo.rb
You cannot run it by just saying todo.rb, unless you place it in your PATH, in which case you can do so from anywhere.
You're missing the ruby at the end of your env command. Did you mean:
#!/usr/bin/env ruby
You need to tell env what executable you're looking for.
(Question Updated)
Are you executing your code like this? todo.rb ? You either need to provide the full path to your script (/home/you/project/todo.rb) or a relative path (./todo.rb) unless that directory is inside your $PATH.
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?
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".
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
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
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.
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.
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
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.