OS X 10.11 $PATH not working? - bash

I can't get the $PATH to work. What am I doing wrong not understanding?
192:~ me$ echo $PATH
/User/me/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
192:~ me$ ruby rtest
ruby: No such file or directory -- rtest (LoadError)
192:~ me$ cd ~/bin
192:bin me$ ruby rtest
Hello World!
why can't it find rtest from anywhere if it's inside my /User/me/bin?
rtest just has 2 lines :
#!/usr/bin/env ruby
puts 'Hello World!'

The $PATH is a list of directories for the shell to search for executables and scripts. In your case, the executable is ruby and rtest is an argument given to ruby. The shell searches only for ruby and runs it, giving rtest to Ruby as a string. Whatever the program does with its arguments is up to to the program. In your case, it is the name of a file for Ruby to find. With no directory name given, it looks in the current directory for a file named rtest. If it's there, it runs it, if not it complains. In your home directory, there is no such file, so ruby complains. When you change to the directory where the file is, it can find it in the current directory, so it runs it.
Try putting the rtest in ~/bin and make it executable:
chmod +x ~/bin/rtest
The first line of your script (#!/usr/bin/env ruby) tells the shell how to run it. You should be able to run it from anywhere:
$ rtest
(Instead of ruby rtest.) It won't execute the script unless you have permission to execute it.

Related

Executable file in ruby

any way to create an 'executable' file in ruby so you can run file.rb with ./file
I know you can just run ruby files ruby filename.rb
I also want to be able to send arguments so ./file argument_1 argument_2
run an ordinate ruby file
Add #! /usr/bin/env ruby to your first line of file.rb file.
giving execute permission to this file: $ chmod +x file.rb
run: $ ./file.rb
run ruby file with parameter from commandline:
use ARGV.
#! /usr/bin/env ruby
puts " parameter0 is: #{ARGV[0]}"
puts " parameter1 is: #{ARGV[1]}"
output:
$ ./test.rb a b
parameter0 is: a
parameter1 is: b
doubli click to run .rb file in windows
install ruby in windows
mouse right click .rb file
open with ...
choose ruby program. as the "default program"
for more details, refer to: https://techforluddites.com/windows-10-change-the-default-programs-for-opening-files/

How to make a Ruby file (in a gem) executable from anywhere like a regular command?

I have a command in my gem: bin/tennis. Currently to execute it you have to cd into the gem and run bin/tennis. I was wondering is it possible to make it work like a regular shell command? i.e able to run it from any dir.
Anything that is globally executable, lies under one of the directories in the $PATH variable (%PATH% on Windows).
For example, $PATH may look like this:
$ echo $PATH
/usr/local/opt/rbenv/shims:/Users/casraf/bin:/Users/casraf/bin:/usr/local/heroku/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/opt/fzf/bin
You may add directories to this list, separating with :, and each of these paths will be looked into when executing a command globally.
So you could either:
Save a copy of the executable in the gem exec dir:
ln -s /your/bin/file $(ruby -rubygems -e 'puts Gem.dir')
This will create a symbolic link to your bin, inside the regular gem executable dir (it should already be in your $PATH, if not, just add it:
export PATH=$(ruby -rubygems -e 'puts Gem.dir'):$PATH
You can put this in your .bashrc or .bash_profile to make sure it happens on every terminal session (if you have a non-standard setup, you may need to find another file to put this in)
Or just add the regular path to your PATH variable:
PATH=/path/to/gem/bin:$PATH

Ruby 1.9 If I have a shebang pointing to ruby, why doesn't the system see it?

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.

-bash: rspec: command not found

I found rspec in /Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib
I even put a path to it in my .bash_profile:
PATH="/usr/local/bin:/Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib:
But when I enter rspec in terminal I get command not found. Even when I'm in the same directory as the file rspec?
Besides being a noob, what am I doing wrong?
*Update:*I was able to execute rspec using "bundle exec rspec" but I'd like to figure out why I can't just use rspec.
Check that your PATH environment variable is what you expect. echo $PATH
Check that your target binary has executable permission. ls -l /Library/Ruby/Gems/1.8/gems/rspec-2.6.0/lib
Check again. which rspec
for a file to be executable as a binary you generally need to:
(a) make the file executable by running chmod 755 /path/to/scripts/awesometask.gem
(b) add a "shebang" to the top of the file (first line) that contains the path of the interpreter for the script: #!/usr/bin/ruby
(c) execute file with full path: /path/to/scripts/awesometask.gem
or
(c1) add the folder where the file is to PATH: PATH=$PATH:/path/to/scripts

Using a #! comment in ruby running in Ubuntu

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.

Resources