Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Alternative to ksh/whence

56 views
Skip to first unread message

Kenneth Brun Nielsen

unread,
Nov 5, 2007, 5:32:37 AM11/5/07
to
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

Stephane CHAZELAS

unread,
Nov 5, 2007, 6:03:33 AM11/5/07
to
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

Kenneth Brun Nielsen

unread,
Nov 6, 2007, 11:27:53 AM11/6/07
to
On Nov 5, 12:03 pm, Stephane CHAZELAS <this.addr...@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

0 new messages