From: Dominic Fandrey on
Some of my shell scripts need to invoke a new shell (e.g. when using
lockf(1) ).

To get the interpreting shell, I use:
interpreter="$(ps -o command -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"

This will yield whatever shell I used to invoke the script on FreeBSD, e.g.
"/bin/sh -f" or "bash".

I wonder whether this is portable, i.e. whether it works on other systems.
From: Kenny McCormack on
In article <hm07pf$f41$1(a)news.doubleSlash.org>,
Dominic Fandrey <kamikaze(a)bsdforen.de> wrote:
>Some of my shell scripts need to invoke a new shell (e.g. when using
>lockf(1) ).
>
>To get the interpreting shell, I use:
>interpreter="$(ps -o command -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"
>
>This will yield whatever shell I used to invoke the script on FreeBSD, e.g.
>"/bin/sh -f" or "bash".
>
>I wonder whether this is portable, i.e. whether it works on other systems.

As you can probably see yourself, anything like that - that depends on
ps and the various other utilities - cannot be proven portable. Now, of
course, the standards jockeys will come in and say that "POSIX requires
blah, blah, blah", but the fact is that "ps" is known to be one of those
loopy utilities that just can't be assumed to be standardized (or
predictable).

If what you are really trying to do is distinguish 'sh' from 'bash',
then I think there are documented "feature test" type solutions - check
for some environment variable or something that (in the most likely
case) 'bash' supplies, but is missing in 'sh'. I think that's the way
to go. A little bit of googling will probably reveal a complete test
suite - to test for all the common shells.

From: Dominic Fandrey on
On 23/02/2010 13:28, Kenny McCormack wrote:
> In article <hm07pf$f41$1(a)news.doubleSlash.org>,
> Dominic Fandrey <kamikaze(a)bsdforen.de> wrote:
>> Some of my shell scripts need to invoke a new shell (e.g. when using
>> lockf(1) ).
>>
>> To get the interpreting shell, I use:
>> interpreter="$(ps -o command -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"
>>
>> This will yield whatever shell I used to invoke the script on FreeBSD, e.g.
>> "/bin/sh -f" or "bash".
>>
>> I wonder whether this is portable, i.e. whether it works on other systems.
>
> As you can probably see yourself, anything like that - that depends on
> ps and the various other utilities - cannot be proven portable. Now, of
> course, the standards jockeys will come in and say that "POSIX requires
> blah, blah, blah", but the fact is that "ps" is known to be one of those
> loopy utilities that just can't be assumed to be standardized (or
> predictable).

Hmm, I see that. I need to use
interpreter="$(ps -o args -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"
to yield the desired results on AIX (luckily this version also works on
FreeBSD).

> If what you are really trying to do is distinguish 'sh' from 'bash',
> then I think there are documented "feature test" type solutions - check
> for some environment variable or something that (in the most likely
> case) 'bash' supplies, but is missing in 'sh'. I think that's the way
> to go. A little bit of googling will probably reveal a complete test
> suite - to test for all the common shells.

I also need to know the parameters, so that a
lockf lockfile $interpreter -c "..."
forwards parameters like -x (for debugging).

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
From: Stephane CHAZELAS on
2010-02-23, 10:39(+01), Dominic Fandrey:
> Some of my shell scripts need to invoke a new shell (e.g. when using
> lockf(1) ).
>
> To get the interpreting shell, I use:
> interpreter="$(ps -o command -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"
>
> This will yield whatever shell I used to invoke the script on FreeBSD, e.g.
> "/bin/sh -f" or "bash".
>
> I wonder whether this is portable, i.e. whether it works on other systems.

ps -o args= -p "$$"
ps -o comm= -p "$$"

would be POSIX.

--
St�phane
From: Dominic Fandrey on
On 23/02/2010 18:23, Stephane CHAZELAS wrote:
> 2010-02-23, 10:39(+01), Dominic Fandrey:
>> Some of my shell scripts need to invoke a new shell (e.g. when using
>> lockf(1) ).
>>
>> To get the interpreting shell, I use:
>> interpreter="$(ps -o command -p $$ | tail -n1 | sed "s, $0${*:+ $*}\$,,1")"
>>
>> This will yield whatever shell I used to invoke the script on FreeBSD, e.g.
>> "/bin/sh -f" or "bash".
>>
>> I wonder whether this is portable, i.e. whether it works on other systems.
>
> ps -o args= -p "$$"
> ps -o comm= -p "$$"
>
> would be POSIX.
>

Oh great, so I don't need tail to get rid of the header!

Thanks!

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?