How to find ruby?

M

Mark Probert

Hi ..

I am not sure of the best way of going about finding the location of Ruby for
a shebang line (*nix execution).

My scripts don't always have control of where Ruby might be installed (that is
the provenance of the destination organization's sysadmin). Some examples
that I have run across include

/usr/local/bin/ruby
/usr/bin/ruby
/opt/ruby1.8/bin/ruby
etc.

So, what do other people normally do when they have this kind of situation
when it comes to the shebang line? I'd rather do it in some generic manner
rather than have a substitution script run at install time (though that is an
option).

Thanks,
 
D

David Mitchell

I do this:

#!/usr/bin/env ruby

For an explanation of why this works, check:

man env

David

Mark said:
Hi ..

I am not sure of the best way of going about finding the location of Ruby for
a shebang line (*nix execution).

My scripts don't always have control of where Ruby might be installed (that is
the provenance of the destination organization's sysadmin). Some examples
that I have run across include

/usr/local/bin/ruby
/usr/bin/ruby
/opt/ruby1.8/bin/ruby
etc.

So, what do other people normally do when they have this kind of situation
when it comes to the shebang line? I'd rather do it in some generic manner
rather than have a substitution script run at install time (though that is an
option).

Thanks,


--
David Mitchell
Software Engineer
Telogis

NOTICE:
This message (including any attachments) contains CONFIDENTIAL
INFORMATION intended for a specific individual and purpose, and
is protected by law. If you are not the intended recipient,
you should delete this message and are hereby notified that any
disclosure, copying, or distribution of this message, or the
taking of any action based on it, is strictly prohibited.
 
J

Jim Menard

Mark said:
My scripts don't always have control of where Ruby might be installed (that is
the provenance of the destination organization's sysadmin).
So, what do other people normally do when they have this kind of situation
when it comes to the shebang line? I'd rather do it in some generic manner
rather than have a substitution script run at install time (though that is an
option).

I use

#! /usr/bin/env ruby

The "env" command finds the ruby command on your path.

Jim
 
N

nobu.nokada

Hi,

At Mon, 30 May 2005 08:05:40 +0900,
Mark Probert wrote in [ruby-talk:143962]:
So, what do other people normally do when they have this kind of situation
when it comes to the shebang line? I'd rather do it in some generic manner
rather than have a substitution script run at install time (though that is an
option).

$ cat a.rb
#!/bin/sh
exec ruby -x "$0" ${1+"$@"}
#! ruby -s
p $foo

$ ./a.rb -foo=123
"123"

But I feel the substitution is better. setup.rb [1] would do
it.

Note that all platforms might not have env under /usr/bin.

[1] http://raa.ruby-lang.org/project/setup
 
D

David Mitchell

Note that all platforms might not have env under /usr/bin.

While it is true that some platforms might not have env in /usr/bin, a
bit of research turns up that all BSD distros have, almost all linux
distros do, and that basically covers 99.99% of the unix machines out
there (including OSX). While substitution will always work, sometimes
you may want a script that requires no installation, in which case using
env is your best bet.


--
David Mitchell
Software Engineer
Telogis

NOTICE:
This message (including any attachments) contains CONFIDENTIAL
INFORMATION intended for a specific individual and purpose, and
is protected by law. If you are not the intended recipient,
you should delete this message and are hereby notified that any
disclosure, copying, or distribution of this message, or the
taking of any action based on it, is strictly prohibited.
 
E

Eric Hodel

Hi,

At Mon, 30 May 2005 08:05:40 +0900,
Mark Probert wrote in [ruby-talk:143962]:
So, what do other people normally do when they have this kind of
situation
when it comes to the shebang line? I'd rather do it in some
generic manner
rather than have a substitution script run at install time (though
that is an
option).

$ cat a.rb
#!/bin/sh
exec ruby -x "$0" ${1+"$@"}
#! ruby -s
p $foo

$ ./a.rb -foo=123
"123"

But I feel the substitution is better. setup.rb [1] would do
it.

Note that all platforms might not have env under /usr/bin.

Even more importantly, not every shell has the same path (cron).
 
J

James Britt

Eric said:
Even more importantly, not every shell has the same path (cron).

Indeed. I've been bitten by this, so I now hard-code the specific Ruby
path into any code called by cron.

James
 
J

Jeremy Tregunna

Hi ..

I am not sure of the best way of going about finding the location of
Ruby for
a shebang line (*nix execution).

My scripts don't always have control of where Ruby might be installed
(that is
the provenance of the destination organization's sysadmin). Some
examples
that I have run across include

/usr/local/bin/ruby
/usr/bin/ruby
/opt/ruby1.8/bin/ruby
etc.

So, what do other people normally do when they have this kind of
situation
when it comes to the shebang line? I'd rather do it in some generic
manner
rather than have a substitution script run at install time (though
that is an
option).

Most modern shells will accept multiple arguments on a shebang line
(some older shells won't, but you shouldn't have a problem).

Use:

#!/path/to/env ruby

where /path/to/env is the location of env(1) (usually /usr/bin).
 
N

nobuyoshi nakada

Hi,

At Mon, 30 May 2005 14:04:06 +0900,
Jeremy Tregunna wrote in [ruby-talk:143980]:
Most modern shells will accept multiple arguments on a shebang line
(some older shells won't, but you shouldn't have a problem).

It isn't a feature of shell, but of kernel, in common. A system
doesn't have it within kernel wouldn't be modern.
Use:

#!/path/to/env ruby

And a problem with this shebang line is you can't put any options to
ruby there (except for *BSD, perhaps).
 
B

Brian Schröder

[snip]
=20
NOTICE:
This message (including any attachments) contains CONFIDENTIAL
INFORMATION intended for a specific individual and purpose, and
is protected by law. If you are not the intended recipient,
you should delete this message and are hereby notified that any
disclosure, copying, or distribution of this message, or the
taking of any action based on it, is strictly prohibited.
=20

I don't think you should post to a newsgroup using this footer. It
seems a bit contradictory to nail a confidential note onto a public
message board.

regards,

Brian

--=20
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/
 
M

Mark Probert

Hi ..

And a problem with this shebang line is you can't put any options to
ruby there (except for *BSD, perhaps).

Many thanks to all.

It seems that I can get away with '/usr/bin/env ruby [opts]' as most of my
scripts run under Solaris or BSD clones (which support the syntax). I need
to look into James' comment re cron on these platforms to double check on
whether I need to go the substitution route.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top