[NTG-context] Dash equivalent of $BASH_SOURCE (for setuptex)

Alan BRASLAU alan.braslau at cea.fr
Wed Nov 21 18:35:31 CET 2012


Mojca,

I'm not sure that I understand the question.
1. dash (Debian version of the real 100% posix-compatible Borne shell)
but also /bin/sh on freebsd and other Unixes (BSD, sysV or POSIX).
It is the "least common denominator" for shell scripts on most (if not
all) unix-like systems, as far as I know.
2. $0 will give the the path of the script, unless it is called
as ./script or script (in the current directory). In this case it
will probably be "./script" or by "pathtoscript/script" in which case
it will return "pathtoscript/script". The current directory can then be
found by `pwd` or $PWD
3 Sourcing the script (not calling it as an executable) as
"source script" or ". script" will return as $0 "/bin/bash" or
"/usr/local/bin/bash" or "/bin/sh" or whatever shell is in use.
This is your problem, I believe.

There does not seem to be a way to determine the path to a script
under a Borne shell if the script is SOURCED and not EXECUTED.
Of course, a script that is executed runs a sub-shell and so this
cannot be used to set variables, even environmental variables, for the
calling shell.

Alan




On Wed, 21 Nov 2012 12:56:54 +0100
Mojca Miklavec <mojca.miklavec.lists at gmail.com> wrote:

> Hi everyone,
> 
> Hans wrote me the following, but I'm unable to figure it out myself.
> The script "setuptex" (which is in fact not urgently needed as one can
> just as well export the right path, but it comes in handy) can be used
> in two ways:
>     (a) . /path/to/context/tex/setuptex
>     (b) . /path/to/context/tex/setuptex /path/to/context/tex
> The first approach only works if the shell interpreting setuptex knows
> where the sourced script is located.
> 
> Currently the script uses the following code and works in Bash, KSH
> (and maybe some other shells as well, but I'm not sure which ones).
> 
> if [ z"$BASH_SOURCE" != z ]; then
>   SCRIPTPATH="$BASH_SOURCE"
> elif [ z"$KSH_VERSION" != z ]; then
>   SCRIPTPATH="${.sh.file}"
> else
>   SCRIPTPATH="$0"
> fi
> 
> Hans had problems with Dash, however, I have no clue how to enable (a)
> in Dash. The (b) approach always works though, so in the worst case
> the answer to Hans would be "provide the path to your tex root, or
> write your own script with hardcoded path ;)", but if anyone has any
> clue if finding path to the executed script is possible in Dash or in
> any other POSIX-compatible way, please let me know.
> 
> Mojca
> 
> On Mon, Nov 19, 2012 at 10:31 AM, Hans Hagen wrote:
> > Hi Mojca,
> >
> > One of the things you need to look into is the first-setup and
> > setup tex scripts (just look at the dash issues thread) .. i have
> > the impression that the setuptex script doesn't find its root (own)
> > under dash because it has different $0 handling (dash is the debian
> > variant of bash, so we're talking of debian installations)
> >
> > Hans



More information about the ntg-context mailing list