Alternative to ksh/whence

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
Kenneth Brun Nielsen [ Mo, 05 November 2007 11:32 ] [ ID #1862386 ]

Re: Alternative to ksh/whence

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
Stephane CHAZELAS [ Mo, 05 November 2007 12:03 ] [ ID #1862388 ]

Re: Alternative to ksh/whence

On Nov 5, 12:03 pm, Stephane CHAZELAS <this.addr... [at] is.invalid> 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
Kenneth Brun Nielsen [ Di, 06 November 2007 17:27 ] [ ID #1863310 ]
Linux » comp.unix.shell » Alternative to ksh/whence

Vorheriges Thema: performance shell scripts
Nächstes Thema: Delete empty lines from a file