你在测试什么?在过去,当我们必须确定我们是在 Kornshell 还是 Bournshell 下运行时,我们可以进行以下测试:
if [ "$RANDOM" = "$RANDOM" ]
then
echo "This is the Bourne shell"
/bin/ksh $* # Script needs the Kornshell
else
echo "This is the Kornshell"
fi
当然,Kornshell 和 Bash 都扩展 $RANDOM(zsh 也是如此)...
好的...您可以通过这种方式找到当前tty上运行的进程:
ps -ft $(tty)
稍微格式化一下:
$ ps -ocommand="" -t$(tty)
login -pf david
-ksh
bash
相当不错。 Shell 以sh 结尾,所以我会做出这样的假设。我只想要以sh结尾的行:
$ ps -ocommand="" -t$(tty) | grep "sh$"
-ksh
bash
是的,我在这个 TTY 中运行了两个 shell。我使用 Kornshell 登录,然后开始使用 bash。让我们把 PID 混在一起:
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$"
62599 -ksh
62855 bash
我们想要 PID 最高的那个
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1
62983 bash
是的,我正在运行 Bash。让我们摆脱 PID:
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1 | sed 's/.* //'
bash
让我们看看它是否适用于各种shell:
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1 | sed 's/.* //'
ksh
Kornshell 很好。
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1 | sed 's/.* //'
zsh
适用于 zsh
$ ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1 | sed 's/.* //'
sh
适用于 Dash 或 Ash
% ps -t$(tty) -opid="" -ocommand="" | grep "sh$" | sort -k1,1nr | head -1 | sed 's/.* //'
Illegal variable name.
在 tcsh 中不起作用。哦,好吧……你不能取悦所有人。