#!/usr/bin/env: No such file or directory

I am new to both linux and ruby. I am using Ubuntu and Ruby 1.9

When I do which ruby, I get /usr/bin/ruby
When I do ruby -v I get ruby 1.9.3p0 (2011-10-30 revision 33570)
[i686-linux]

When I try to kick off a simple script

#!/usr/bin/env

puts “Hello”

I get

./test.rb: line 1: #!/usr/bin/env: No such file or directory
./test.rb: line 4: puts: command not found

Why?

Thanks

Todd

as others suggested you can do the following:

Link directly to your ruby executable:
#!/usr/bin/ruby

This line is called the shebang line, whenever your computer tries to
read
a script, this line tells the computer what program to run this script
with. If you shebang to your ruby program directly, there is no issue.
But
if you share the code, or install ruby to a different directory later
on,
you have to change the line, some people might be using RVM, or have it
installed in #!/usr/local/bin/ruby instead. So we use the ENV program to
find the appropriate version of ruby in the PATH. #!/usr/bin/env ruby
says
use the ruby that is returned by the env program. this way ENV returns
the
ruby that is set for use in your systems path. This changes computer to
computer, so it is the most standard way of writing the shebang line to
make sure your code works on multiple machines. #!/usr/bin/env just
dumps
your entire environment.

To see what I mean, type env in terminal and console. Then type env ruby
and see what happens.

Cheers,

Richard

On Wed, Jun 26, 2013 at 9:04 PM, Duong Quang Ha
[email protected]wrote:

I am new to both linux and ruby. I am using Ubuntu and Ruby 1.9

Todd


Richard Wilson
Proteous Trading Group
ph. - (604) 842 5318
Skype - r.crawfordwilson
email - [email protected]

This email may contain confidential and/or privileged information. If
you
are not the intended recipient or have received this email in error,
please
notify the sender immediately and destroy this email. Any unauthorized
copying, disclosure or distribution of the information contained on this
email is prohibited”.

On 27/06/13 15:58, Todd S. wrote:

puts “Hello”

Todd

#!/usr/bin/envruby

Hope that helps.

Sam

Hello,

Shebang line should like “#! /usr/bin/env ruby” or “#! <path to your
ruby VM i.e /usr/bin/ruby>”. This line tell shell which program to
run.

Regards,

Hi Todd!

!#/usr/bin/env ruby

it needs the token (i.e. program executable name) in front of the env
token. This makes your script portable.

You will want to make it exectutable which is done with the command
chmod
like so:

% chmod +x /path/to/your-program

from there to exectute it you can do it one of three ways.

  1. simply use dot slash in front of the program like: ./prog
  2. type the whole path like: /home/todd/prog
  3. set up a bin directory and set up your the PATH variable in your
    shells
    rc file.

The last one is your homework =) With that you can just run your script
from anywhere and even incorporate it into other scripts and so on.

Congratulations and welcome to the wonderful world of UNIX and Ruby
Programming =)
Oh and one last word of advice. AUTOMATE EVERYTHING!

~Stu (15 years hacking shells networks and BSDs and GNUs )

Well that’s embarrassing. It’s hash bang… I had reversed it while
typing
to fast. My apologize.

Todd to know more about chmod read the man page:
% man chmod

It stands for change mode. Basically it’s turning on the execute bit
when
you use +x.

~Stu

Thanks for the responses. I should have been a bit more clear about my
UNIX skills. Not great, but I can move around and set permissions and
look at things like environment variables. So I had already set things
to execute. I still can’t see what is up. Here is the code(again) and
files and symbolic links & path & permissions

Here is my beautiful code:

#!/usr/bin ruby

puts “Hello world!”

which produces these errors:

./test.rb: line 1: #!/usr/bin: No such file or directory
./test.rb: line 3: puts: command not found

/usr/bin is in PATH so it should not have a problem there:

/usr/lib/lightdm/lightdm:
/usr/local/sbin:
/usr/local/bin:/usr/sbin:
/usr/bin:/sbin:/bin:
/usr/games:
/usr/bin/ruby

ruby points, through symbolic links, to /usr/bin/ruby1.9.1

lrwxrwxrwx 1 root root 22 Jun 20 16:30 ruby -> /etc/alternatives/ruby

lrwxrwxrwx 1 root root 18 Jun 20 16:30 ruby -> /usr/bin/ruby1.9.1

-rwxr-xr-x 1 root root 5552 Mar 22 12:14 ruby1.9.1

And the code is here:

-rwxrwxrwx 1 john john 41 Jun 27 18:41 test.rb

I am going to /home/john to run the file ./test.rb

And everything is executable

On 28/06/13 13:55, Todd S. wrote:

puts “Hello world!”
/usr/local/bin:/usr/sbin:

-rwxr-xr-x 1 root root 5552 Mar 22 12:14 ruby1.9.1

And the code is here:

-rwxrwxrwx 1 john john 41 Jun 27 18:41 test.rb

I am going to /home/john to run the file ./test.rb

And everything is executable

#!/usr/bin/env ruby

or

#!/usr/bin/ruby

Prefer the former over the latter =]

Sam

I added /env (and should have known better) and still get the same
errors

#!/usr/bin/env ruby

puts “Hello world!”

./test.rb: line 1: #!/usr/bin/env: No such file or directory
./test.rb: line 3: puts: command not found

On 28/06/13 14:39, Todd S. wrote:

Try this in your shell:

which env

If you get nothing, try this:

which ruby

If you get nothing, you are stuck (your $PATH is probably broken).

echo $PATH

Sam

On Thu, Jun 27, 2013 at 7:55 PM, Todd S. [email protected]
wrote:

Thanks for the responses. I should have been a bit more clear about my
UNIX skills. Not great, but I can move around and set permissions and
look at things like environment variables. So I had already set things
to execute. I still can’t see what is up. Here is the code(again) and
files and symbolic links & path & permissions

Here is my beautiful code:

#!/usr/bin ruby

Change that to: #!/usr/bin/env ruby

/usr/lib/lightdm/lightdm:
lrwxrwxrwx 1 root root 18 Jun 20 16:30 ruby → /usr/bin/ruby1.9.1
And everything is executable


Posted via http://www.ruby-forum.com/.

Everything looks fine except for the one change I mentioned above.

With the shebang line, you are telling bash to execute “/usr/bin/env”,
passing it one parameter, “ruby”. The “env” program will then launch the
appropriate “ruby” executable.

The reason for this is that “shebang” line must specify a complete path
to
an executable (either an absolute path or one relative to the current
working directory; relative paths are problematic though so everyone
uses
absolute paths). Since the “ruby” executable may be installed in a
different location on different systems, the convention is to instead
run
ruby via the “env” command for which it is safe to assume it’ll be
available in the /usr/bin directory.

The “env” command lets you run other commands in a modified environment.
However, in this case, it’s just being used for the fact that it’ll look
up
whatever command you tell it to run using the PATH environment variable.
So, as long as an executable (or a symlink to one) is in one of the
directories in your PATH (which is the case on your system) then “env”
can
run it.

You can try it manually inside your shell (the “^D” mean, type CTRL-D;
you
won’t actually see the “^D” characters):

$ /usr/bin/env ruby
puts “Hi”
^D
Hi
$

The above runs ruby which reads it’s input (ruby code) from the
terminal.
The CTRL-D tells ruby it’s reached the end of the file so it will exit,
at
which point it will run whatever ruby code you entered (outputting “Hi”
to
the terminal).

This works

$ /usr/bin/env ruby
puts “Hello world”
^D

$ ls -la /usr/bin/env Gives

-rwxr-xr-x 1 root root 22060 Nov 19 2012 /usr/bin/env

which env gives /usr/bin/env
which ruby gives /usr/bin/ruby
ruby -v gives ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]

On Thu, Jun 27, 2013 at 8:39 PM, Todd S. [email protected]
wrote:

What’s the output of:

$ ls -la /usr/bin/env

What happens if you try the by-hand experiment I mentioned in my last
message:

$ /usr/bin/env ruby
puts “Hello world”
^D

(where the ^D means type CTRL-D)

What happens when you type ruby in front of it like:

% ruby test.rb

I changed my text editor and it works. Hidden characters?

On 2013-06-29, at 7:15 PM, Todd S. [email protected] wrote:

I changed my text editor and it works. Hidden characters?


Posted via http://www.ruby-forum.com/.

It might be the line endings.

If I have “DOS” style line endings in a file then on Linux I see:

vagrant@precise64:/vagrant$ cat try.rb
#!/usr/bin/env ruby

puts “Hello”
vagrant@precise64:/vagrant$ ls -l try.rb
-rwxrwxr-x 1 vagrant vagrant 37 Jun 29 23:38 try.rb
vagrant@precise64:/vagrant$ ./try.rb
: No such file or directory

On my Mac trying the same thing I get a more obvious clue:

ratdog:vagrant_book_example mike$ ./try.rb
env: ruby\r: No such file or directory

Which tells me there is a \r at the end of the #! line. I often use the
file command to check, again on Linux:

vagrant@precise64:/vagrant$ file try.rb
try.rb: a ruby\015 script, ASCII text executable, with CRLF line
terminators

The CRLF is one give-away.

Hope this helps,

Mike

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

I suspect BOM.

Try:
$ od -tx1z test.rb