g4u*_*r4v 34 linux shell-script
据我所知,确定我们echo $0在shell中使用的当前shell。相反,我希望我的脚本检查它正在运行的 shell。所以,我尝试$0在脚本中打印,它返回了它应该的脚本名称。所以,我的问题是如何找到我的脚本在运行时运行在哪个 shell 中?
Sté*_*las 60
也许不是你所要求的,但这应该在某种程度上可以识别当前解释它的口译员
osh),bash),ksh88, ksh93, pdksh, mksh),zsh,posh),yash),rc 贝壳,akanga 贝壳,wish TCL翻译,tclsh TCL翻译,expect TCL翻译,cmd.exe,command.com(MSDOS、FreeDOS...)。'echo' +"'[{<?php echo chr(13)?>php <?php echo PHP_VERSION.chr(10);exit;?>}\
@GOTO DOS [exit[set 1 [[set 2 package] names];set 3 Tcl\ [info patchlevel];\
if {[lsearch -exact $1 Expect]>=0} {puts expect\ [$2 require Expect]\ ($3)} \
elseif {[lsearch -exact $1 Tk]>=0} {puts wish\ ($3,\ Tk\ [$2 require Tk])} \
else {puts $3}]]]' >/dev/null ' {\">/dev/null \
">"/dev/null" +"\'";q="#{",1//2,"}";a=+1;q='''=.q,';q=%{\"
'echo' /*>/dev/null
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 (! $?version) set version=csh;exec echo $version
:DOS
@CLS
@IF NOT "%DOSEMU_VERSION%"=="" ECHO DOSEMU %DOSEMU_VERSION%
@ECHO %OS% %COMSPEC%
@VER
@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 PO po;t YA ya
case `(typeset -Z2 b=0;$e $b)2>$n` in 00) (eval ':${.}')2>$n&&eval '
$e ksh93 ${.sh.version}';t K pdk;$e ksh88;;esac;case `(eval '$e ${f#*s}$($e 1
)$((1+1))')2>$n` in e12)$e POSIX shell;;esac;$e Bourne-like shell;: }
print "ruby ",RUBY_VERSION,"\n";exit;' ''';import sys
print("python "+sys.version);z='''*/;
s="";j="JavaScript";if(typeof process=="object"){p=console.log;p(process.title
,process.version)}else{p=print;p((f="function")==(t=typeof version)?"string"==
typeof(v=version())?v:(typeof build!=f?"":s= "SpiderMonkey ")+j+" "+v:(t==
"undefined"?j+"?":version)+"\n");if(s)build()}/*
:FIN } *///'''
Run Code Online (Sandbox Code Playgroud)
我在 2004 年左右在 usenet 上发布了which_interpreter 脚本的初始版本。Sven Mascheck 有一个(可能对您更有用)脚本,称为whatshell,专注于识别 Bourne-like shell。您还可以在那里找到我们两个脚本的合并版本。
phe*_*mer 35
在 linux 上,您可以使用/proc/PID/exe.
例子:
# readlink /proc/$$/exe
/bin/zsh
Run Code Online (Sandbox Code Playgroud)
Jen*_*ens 13
这是我在我的 .profile 中用来检查我工作的系统上的各种 shell 的内容。它并没有在 ksh88 和 ksh93 之间做出很好的区分,但它从来没有让我失望。
请注意,它不需要单个叉子或管道。
# Determine what (Bourne compatible) shell we are running under. Put the result
# in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type.
if test -n "$ZSH_VERSION"; then
PROFILE_SHELL=zsh
elif test -n "$BASH_VERSION"; then
PROFILE_SHELL=bash
elif test -n "$KSH_VERSION"; then
PROFILE_SHELL=ksh
elif test -n "$FCEDIT"; then
PROFILE_SHELL=ksh
elif test -n "$PS3"; then
PROFILE_SHELL=unknown
else
PROFILE_SHELL=sh
fi
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15634 次 |
| 最近记录: |