Proper usr/bin/env ruby shebang

On 1 feb, 19:51, “Garance A Drosehn” [email protected] wrote:

Well, on many of my systems, if /usr is not mounted, then ‘ruby’ itself
is also not available! :slight_smile:

I have a bigger concern with using /usr/bin/env for this. The
/usr/bin/env trick will only work if ruby IS in the PATH of the
person who is running the ruby script, and if that version of ruby is
the version expected by the script. On many systems that is a safe
assumption, but it isn’t always true.

It should be.

Consider something like MacOS 10, for instance, where Apple
ships one version of ruby in /usr/bin, but many people will install a
newer version in /opt/local/bin. I’ve had some scripts fail because
the user running them didn’t have /opt/local/bin in their PATH.

More the reason to use /usr/bin/env. That way, you are not hard-
coding the script to a specific version of ruby.
If you had done the opposite and had used #! /usr/bin/ruby (or
whatever location macs put ruby by default ), your users would have
never been able to use a different version of ruby without having to
manually change the path to ruby in all scripts or type “ruby” itself.
If your users have path issues, your likely problem is not /usr/bin/
env but a bad system-wide .bashrc or .tcshrc configuration file that
is not adding the path automatically (a sysadmin issue) or some lack
of knowledge on the user’s part who probably overrode what the
sysadmin did by setting path improperly. If it is a sysadmin issue,
talk to your sysadmin (and probably, start shopping for a new
sysadmin :). If it is a user issue, as it is more likely, teach him
how to change variables without overriding system-wide configurations
(ie. PATH=stuff:$PATH instead of just PATH=stuff).
Another way this happens is when a user wants to use an unpopular (or
an incompatible) shell within the company (say, zsh in a place where
tcsh is the default) and as such he does not inherit the settings the
sysadmin sets up for him. A good way around this (I have found over
the years) is to eventually handle all environment path changes with a
custom script (usually written in perl or ruby) rather than by using
the shell’s built-in commands (setenv/export/etc). The ruby script is
the one in charge of detecting the shell the user is using and spits
out the correct setenv commands for it (this output is evaled thru an
alias).
See for example (this only handle output for a single shell, thou):
Erco's Homepage: Unix Tools (epath)
or if you want ruby code:
epath - Free OS Utils Applications Downloads
http://rubyforge.org/projects/ggenv/ ( a simpler library )

Nobuyoshi N. wrote:

Do not rely on /usr/bin/env, it may not exist.

Also, /usr/bin/env relies on the $PATH env variable,
so if you try to run this as a cron job it will fail
because $PATH is not set.

Daniel

On 1 feb, 19:22, Will P. [email protected] wrote:

  • Will

env in bin is definitively non-standard unix, so that platform is
likely to end with a sudo ln -s /bin/env /usr/bin almost immediately.
I was not aware of any unix platform without env. What platform are
you on btw? Obviously windows does not have env, but you solve issues
there with file associations or by using some unix environment (like
cygwin, etc).

On Feb 3, 12:25 am, Daniel DeLorme [email protected] wrote:

Nobuyoshi N. wrote:

Do not rely on /usr/bin/env, it may not exist.

Also, /usr/bin/env relies on the $PATH env variable,
so if you try to run this as a cron job it will fail
because $PATH is not set.

Daniel

Again, that’s probably bad sysadmin or user error in setting up cron.

grep PATH /etc/crontab
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

If you run your own cron jobs as a normal user, you should create your
own similar crontab file. You can do so with:

crontab /etc/crontab
crontab -e # to edit it

(Depending on your linux flavor, your user cron file will be stored
in /var/spool/cron/crontabs/ (Unix/Slackware/*BSD),
/var/spool/cron/ (RedHat) or /var/cron/tabs/ (SuSE).

On 2/1/07, Garance A Drosehn [email protected] wrote:

I also have to write ruby or perl scripts which have to work on a variety
of
unix-based operating systems. What I have come up with is the following.
I’m sure that it will not work on some operating system, but it works on
the
dozen operating systems that I have to care about, in all situations that
I
have cared about.

Some people have mentioned that my sample-code was reformatted into
something close to useless by their email (or web-ized mail) clients.
So
here is a very simple “Hello World” script which includes my
ruby-finding
strategy, along with some comments about how it works:

http://people.freebsd.org/~gad/tools/safe_ruby_script

(that is the actual script, and not a web page which displays the
script, so
your web browser might simply download it as a file to your desktop)

I realize that it isn’t a truly 100% perfect solution for every possible
situation,
but it has worked well over the past few years for me, writing scripts
which
had to work reliably on several different platforms.