Cyrille Lefevre wrote:
>
> Terrence J. Doyle a écrit :
>> Cyrille Lefevre wrote:
>>> how about :
>>>
>>> if [ "${RANDOM}" != "${RANDOM}" ]; then
>>> is='ksh'
>>> else
>>> is='sh'
>>> fi
>
> that was the short answer...
>
>> The $(id) and ${RANDOM} tests both give a false positive if ksh, bash or
>> zsh is masquerading as sh. Nowadays, it's common to find that /bin/sh is
>> actually bash and that there's no true Bourne Shell on the system. If
>> you want to know the basename of the shell executable you're running,
>> the classic $0 test does the job. For /etc/profile that should be good
>> enough. But, if you really want to know which shell you're currently
>> running, this version test is probably what you're looking for:
>>
>> if ( [ -n "${.sh.version}" ] ) 2>&- # ${.sh.version} causes an error
>> message in non-ksh shells.
>> then
>> is=ksh
>> elif [ -n "$BASH_VERSION" ]
>> then
>> is=bash
>> elif [ -n "$ZSH_VERSION" ]
>> then
>> is=zsh
>> else
>> is=sh
>> fi
>
> your test may cause script to exit if set -u is true due to 1st line,
Actually, without the subshell the test in the first line would cause my
script to exit whether or not set -u is in effect because ${.sh.version}
is essentially a syntax error in non-ksh shells. That's why I nested the
test in a subshell. As long as the subshell is there the script works,
and it doesn't exit unexpectedly in any shell.
However, I just realized that the $0 test is the test that is truly
fooled when bash, ksh, or zsh is masquerading as sh. I was wrong when I
said that $(id) and ${RANDOM} tests both give a false positive in this
situation. In that respect, my script is just a reiteration of the point
you were trying to make. I guess the point I was trying to make is that,
on a system where /bin/sh is not actually Bourne Shell, with either the
$(id) test or the ${RANDOM} test you can't say with certainty that the
shell is ksh. The shell might also be bash or zsh (or pdksh as you point
out below).
> how about this, the long answer, which I use for years :
>
> if [[ -n ${BASH_VERSION:-} ]]; then
> is='bash'
> elif [[ -n ${KSH_VERSION:-} ]]; then
> is='pdksh'
> elif [[ -n ${ZSH_VERSION:-} ]]; then
> is='zsh'
> elif [[ ${RANDOM:-} != ${RANDOM:-} ]]; then
> if [[ ${SECONDS:-} = *'.'* ]]; then
> is='ksh93'
> else
> is='ksh88'
> fi
> else
> is='posixsh'
> fi
>
> note that $SECONDS is a float under ksh93 and an integer under ksh88 :)
> non posix sh don't have $RANDOM as I far as I know.
The :- added to the variable expansions is a definite improvement over
my script. That will silence the error messages from set -u. However,
the double-bracket ([[ ]]) tests won't work in traditional Bourne Shell
(or Almquist Shell (ash) for that matter). To make the script truly
universal (as far as this discussion is concerned) you should use
single-bracket ([ ]) tests until the $SECONDS test where you know you're
not dealing with Bourne Shell.
Terrence Doyle
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users