Alternative to ksh/whence

Alternative to ksh/whence

am 05.11.2007 11:32:37 von Kenneth Brun Nielsen

At my work, we have a fancy tool setup. At some point a script (/bin/
sh -u) is run including the command:

progpath='/bin/ksh -c "whence $0"'

This command does not work. The problem is, that we do not use ksh at
this place (not installed, and I won't dare to try, unless you tell me
otherwise..). The context of the command clearly reveals the intention
of it, i.e. to get the path of the run script.

Do you guys know an alternative command (available in sh/csh/bash)?
I'd prefer not to code an absolute path into the script...

Best regards,
Kenneth

Re: Alternative to ksh/whence

am 05.11.2007 12:03:33 von Stephane CHAZELAS

2007-11-05, 10:32(-00), Kenneth Brun Nielsen:
> At my work, we have a fancy tool setup. At some point a script (/bin/
> sh -u) is run including the command:
>
> progpath='/bin/ksh -c "whence $0"'
>
> This command does not work. The problem is, that we do not use ksh at
> this place (not installed, and I won't dare to try, unless you tell me
> otherwise..). The context of the command clearly reveals the intention
> of it, i.e. to get the path of the run script.
[...]

But is not correct. $0 shouldn't be looked up in $PATH unless it
can't be found in the current directory.

If your /bin/sh is a Unix conformant shell, try:

progpath=$(
progpath=$0
case $0 in
(*/*) ;; (*)
[ -e "$progpath" ] || progpath=$(command -v -- "$progpath") || exit
esac
cd -P -- "$(dirname -- "$progpath")" && pwd -P || exit
) || exit
progpath=$progpath/$(basename -- "$0")

The above doesn't work if the dirname or basename of $0 ends in
a newline character.

--
Stéphane

Re: Alternative to ksh/whence

am 06.11.2007 17:27:53 von Kenneth Brun Nielsen

On Nov 5, 12:03 pm, Stephane CHAZELAS wrote:

> If your /bin/sh is a Unix conformant shell, try:
>
> progpath=$(
> progpath=$0
> case $0 in
> (*/*) ;; (*)
> [ -e "$progpath" ] || progpath=$(command -v -- "$progpath") || exit
> esac
> cd -P -- "$(dirname -- "$progpath")" && pwd -P || exit
> ) || exit
> progpath=$progpath/$(basename -- "$0")
>
> The above doesn't work if the dirname or basename of $0 ends in
> a newline character.

Thanks mate. I will implement this and hope it works. Thanks for your
effort - it's appreciated :)

/Kenneth