On Thu, 18 Feb 1999, Martin Buchholz wrote:
I tried to solve this problem in the distant past by adding this
hack
to all the scripts in lib-src:
PATH=${PATH:-/usr/bin:/bin}:`dirname $0 2>/dev/null`; export PATH
This hack requires that $0 contain the full path to the script, and
that dirname exists. Why would this this not be the case?
[ I didn't realize there would be a quiz. ]
argv[0] is not required to contain anything meaningful. Your shell may
pass more or less useful information in $0. Nice shells pass more. From
execv man page (Linux 2.0.35), typos and all:
The execv and execvp functions provide an array of point-
ers to null-terminated strings that represent the argument
list available to the new program. The first argument, by
convention, should point to the file name associated with
the file begin executed. The array of pointers must be
terminated by a NULL pointer.
You can execl("/bin/ls", "ha-ha", "-l", 0), though it might
give ls a
headache if it doesn't know its name. Mostly, $0 will be either a
relative or absolute path to yourself, but it depends on the shell/execer.
A perl module (FindBin) does some further sanity checks: Check if $0
exists. Make it absolute if relative. If $0 is only a basename "cat",
search for $0 in $PATH to get the fullpath of $0 (this last check would be
pointless if trying to get the dir of $0, of course). Fail if all of
these attempts fail.
-Justin
vallon(a)mindspring.com