--
John
> Is there any way to identify which shell a script is running under?
>
Most (all?) shell scripts have the specific shell to use as their first line
- something like "#!/bin/bash", so the shell is known beforehand. This is
necessary because the syntax is different for different shells.
Is this what you mean?
P.
Not really. Sometimes programs can get renamed... I was wondering if
there was something like 'sh --version' which would reveal the real
identity of the shell.
>P.
>
>
--
John
(faui05) [~] echo $BASH_VERSION
2.05b.0(1)-release
(faui05) [~] echo $BASH_VERSINFO
2
Greetings,
Thomas
This is a thorny issue that has been discussed here more than
once. The following threads discussed the subject:
Subject: of ~/.profile, bourne shell, and ksh93's ${.sh.version}
Subject: Shell Question
You should also search the archives for scripts by Brian Hiles and
Heiner Steven (and maybe others) that attempt (not entirely
successfully) to do what you want.
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2002, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Something like $SHELL?
> $ echo ${SHELL##*/}
> bash
Problem with that line is that it will probably not work in other
shells. I dont have any other than bash here so I can't test.
--
Friendly,
Rikard
Another problem is that it tests the name of the shell as it was
installed, and it is quite common for either bash or the korn shell
to be installed as /bin/sh, which we might naively expect to be
the bourne shell.
And another is that it does not test which version of which shell
is installed. More recent shells can add significant functionality in
new versions.
The correct approach is probably to test for the features
that the OP wants to use, rather than worry about the name
of the shell.
This is the approach generally taken with open source software
(following the Gnu tools) which, when configuring before building
on a particular platform, checks for the presence or absence of
particular features.
John.
> "Rikard Bosnjakovic" <b...@jaevel.as> wrote in message news:slrnb1umh...@jaevel.as...
>
>>>Is there any way to identify which shell a script is running under?
>>
>>Something like $SHELL?
>>
>>>$ echo ${SHELL##*/}
>>>bash
>>
>>Problem with that line is that it will probably not work in other
>>shells. I dont have any other than bash here so I can't test.
>
> Another problem is that it tests the name of the shell as it was
> installed, and it is quite common for either bash or the korn shell
> to be installed as /bin/sh, which we might naively expect to be
> the bourne shell.
>
> And another is that it does not test which version of which shell
> is installed. More recent shells can add significant functionality in
> new versions.
>
> The correct approach is probably to test for the features
> that the OP wants to use, rather than worry about the name
> of the shell.
I fully agree with you. Why should a script terminate if it
was written for ksh, but is run with BASH? Many (not all) ksh
scripts will work nevertheless. Checking for the needed features
is best -- although it's error prone and can make the script
code more complicated.
Having said that, here is a version of the "shtype" script
that tries to find out, what shell it is run with.
It punctually tests for certain features which are known
to run only with a certain shell.
It does not yet work for "ash". Does anybody have an idea,
how it should check for ASH?
>>>>>>>>>>( START shtype )>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# shtype - determine shell type (hs '96, 1.3)
# Heiner Steven (hei...@hsysnbg.nbg.sub.org), Public Domain.
#
# Note: this script *must* start with a '#' character, because otherwise
# "csh" may use "sh" to run it.
#
# Knows sh, ksh, ksh93, csh, tcsh, bash, zsh
#
# Thanks to
# Dave Plonka <dev...@mfa.com> (BSH, POSIXSH)
#
# Check for Bourne shell or C shell dialect
set x = 1
test "$x" = 1 && goto CSH
# Bourne shell dialect
# Don't use external commands
PATH=; export PATH
# The standard Bourne shell does not know how to remove parts
# of a variable with ${VAR%%pattern}.
# Bug: "ash" terminates the script with "Bad substitution". How can I
# prevent this?
x="A.B"
x=`(echo ${x%%.*}) 2>/dev/null`
if [ "$x" = A ]
then
# bash/zsh or ksh?
# bash/zsh use $[...] for arithmetic evaluation.
x=`(echo $[0+1]) 2>/dev/null`
if [ "$x" = 1 ]
then
# zsh tests with ${+x} if variable x is set
x=0
x=`(echo ${+x}) 2>/dev/null`
if [ "$x" = 1 ]
then
echo "ZSH $ZSH_VERSION"
else
echo "BASH $BASH_VERSION"
fi
else
# ksh or ksh93?
# ksh93 can extract substrings from a variable.
x=AB
x=`(echo ${x:1:1}) 2>/dev/null`
if [ "$x" = B ]
then
echo "KSH93 ${.sh.version}"
else
echo KSH ${KSH_VERSION-$KSH_VERSION}
fi
fi
else
echo BSH
fi
exit 0
CSH:
# C-Shell dialect
# tcsh has a bindkey command
if { bindkey >& /dev/null } then
echo "TCSH $version"
else
echo CSH
endif
exit 0
>>>>>>>>>>( END shtype )>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
The original of this script is always available at the
SHELLdorado:
http://www.shelldorado.com/scripts/cmds/shtype
Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner...@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/