There have been many attempts to answer this, none has been
totally successful.
The best way is to test for the capabilities you want or need in
your script.
Some shells have a variable that gives you the information; bash
has BASH_VERSION, pdksh has KSH_VERSION, ksh93 has a variable that
uses an illegal name (${.sh.version}), so is useless in other
shells; pdksh and ksh88 will print an error message and continue,
but a Bourne shell or bash will exit.
If you are more specific about what you need to do, I am sure
someone will come up with a method to do what you need.
--
Chris F.A. Johnson http://cfaj.freeshell.org
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
For a more general approach to the problem, the following
script will try to tell which interpreter it is run by. Example
of result with different interpreters:
akanga: akanga
ash: ash/BSD sh
bash: bash 2.05b.0(1)-release
bash1: bash 1.14.7(1)
bash-2.02: bash 2.02.1(1)-release
bsh: Bourne-like shell
cmd.exe: Windows_NT D:\WINDOWS\system32\cmd.exe
csh: csh
cygwin-sh: ash/BSD sh
es: es version 0.9-beta1 12-August-1997
es-0.67: es version 0.67: 30 Sep 1992
expect5.28: expect 5.28.1 (Tcl 8.0p2)
ksh: ksh88
ksh93: ksh93 Version M 1993-12-28 n+
perl: perl 5.8.0
pdksh: pdksh @(#)PD KSH v5.2.14 99/07/13.2
python: python 2.0 (#2, Dec 17 2000, 18:49:55)
[GCC 2.95.2 19991024 (release)]
rc: rc 1.7 $Release: @(#)rc 1.7 2002-06-18 $
rc-1.4: rc
ruby: ruby 1.6.7
tclsh: tcl 8.0p2
tcsh: tcsh 6.12.00 (Astron) 2002-07-23 (i386-intel-linux) options 8b,nls,dl,al,rh,color,filec
wish: wish (Tcl 8.0p2, Tk 8.0)
zsh: zsh 4.1.1-dev-1
zsh-3.0.7: zsh 3.0.7
zsh-4.0.4: zsh 4.0.4
Uv5 sh: possibly pre-Bourne UNIX V1-6 shell
Uv6 sh: possibly pre-Bourne UNIX V1-6 shell
Uv7 sh: Bourne-like shell without function
'echo' +"'\
@ goto dos \
[exit[if {[lsearch -exact [package names] Expect]>=0} {puts expect\ [\
package require Expect]\ (Tcl\ [info patchlevel])} elseif {[lsearch -exact [\
package names] Tk]>=0} {puts wish\ (Tcl\ [info patchlevel],\ Tk\ [package \
require Tk])} else {puts tcl\ [info patchlevel]}]]' >/dev/null ' {\">/dev/null \
">"/dev/null" +"\'";q='''=.q,';q=%!\"
echo ">/dev/null;status=0;@ {status=1};*=(" '$' ");~ $status 1&&{e='"\
"';eval catch $2 ^'&version {eval ''echo <='^ $2 ^'&version''}';exit};e='"\
"';if (eval '{let ''a^~a''} >[2] /dev/null'){e='"\
"';exec echo akanga};eval exec echo rc $2 ^ version;\" > /dev/null
: #;echo possibly pre-Bourne UNIX V1-6 shell;exit
if { bindkey >& /dev/null } then
exec echo $version
else
exec echo csh
endif
: dos
@echo off
cls
echo %OS% %COMSPEC%
goto fin
", unless eval 'printf "perl %vd\n",$^V;exit;'> "/dev/null";eval ': "\'';
=S"';f=false e=exec\ echo n=/dev/null v=SH_VERSION;`(eval "f() { echo :
};f")2>$n` $f||$e Bourne-like shell without function
case `(: ${_z_?1}) 2>&1` in 1) $e ash/BSD sh;;esac;t(){
eval "\${$1$v+:} $f &&exec echo ${2}sh \$$1$v";};t BA ba;t Z z;t K pdk
a=:\ $f;case `(typeset -Z2 b=0;$e $b)2>$n` in 00) (`eval '$e ${a:1}'`:)2>$n \
||eval '$e ksh93 ${.sh.version}';$e ksh88;;esac;case `(eval '$e ${a#*s}$($e 1
)$((1+1))')2>$n` in e12)$e POSIX shell;;esac;$e Bourne-like shell;: !
print "ruby ",VERSION,"\n";exit;' '''
import sys;print "python",sys.version;z='\
: fin } '
--
Stéphane ["Stephane.Chazelas" at "free.fr"]
Stephane CHAZELAS <this.a...@is.invalid> wrote in message news:<slrnc1tl6j.42.s...@spam.is.invalid>...
~$ bash -c 'echo $SHELL'
/bin/zsh
The SHELL variable is set by no shell I know, it's just used
by applications such as vi, ftp, xterm so that they know which
shell to run when requested. It's generally set by "login" based
on your passwd entry, but can be modified at your will.
I can't use it to test pdksh:
$ cat xx.sh
#!/usr/bin/pdksh
whichsh ## contains the script below
## EOF
$ xx.sh
bash 2.05b.0(2)-release
And it cannot be included in the script being tested, as it exits,
and will not execute any subsequent commands.
It cannot be sourced, as it will exit the calling shell.
--
As it is, it is to be tested as:
interpreter whichsh
or
interpreter < whichsh
Or copy-paste it within the interpreter (yes, it will exit it,
for most interpreters).
It's not really intented to be useful actually. That was just
much fun to have a script that can be interpreted by such a wide
range of different interpreters with totally different syntaxes.
It can be /easily/ modified so that it does other things
whatever the interpreter (that's the only /useful/ usage of it I
can think of).
Awesome stuff! Did you wrote this? This should be one of the best "many
languages single source" program I've seen.
--
carlos ** http://cgd.sdf-eu.org
Well, for most unixes shell the following might do the trick:
ps | grep $$ | awk '{print $NF}'
And if it returns "sh"? That can be any one of several different
shells. On *BSD it's ash, on Linux it's usually bash, on others
it's a Bourne shell, etc..
If it returns "bash" or "ksh", which version is it?
Give it a break, will you? Probably the guy just wants to know which
/bin/something he's running...
ps -o comm= -p "$$"
or
ps -o args= -p "$$"
with POSIX compliant ps.
Note that awk being a superset of grep, you generally don't need
to pipe them together. Grepping for "$$", you may retrieve every
line that contain $$, not only those for which the pid is $$.
Yes, one year ago. I added languages one after the other,
starting with bourne-like/csh/Thomson/rc shells, adding tcl
ones (tclsh, wish, expect), ruby and COMMAND.COM in the end. I
couldn't manage to have it working with awk. I didn't even try
with scsh, ch or esh, but it becomes very tricky to add stuff,
as for each byte you add, you have to make sure it doesn't break
either interpreter.
> This should be one of the best "many
> languages single source" program I've seen.
Thank you. I have to admit I'm quite proud of that otherwise
useless script ;).
The following script does not know as many different shell
dialects as Stephane's, but it's nevertheless useful:
http://www.shelldorado.com/scripts/cmds/shtype
Example:
$ shtype
KSH @(#)PD KSH v5.2.14 99/07/13.2
Heiner
--
___ _
/ __| |_ _____ _____ _ _ Heiner STEVEN <heiner...@nexgo.de>
\__ \ _/ -_) V / -_) ' \ Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_| http://www.shelldorado.com/