根据 shell 类型 bash 与 ksh 有条件地绕过命令

问题描述 投票:0回答:1

我的脚本中有这一行。

if [ "$(ps -p $$ -o comm=)" = "bash" ]; then
    exec 1> >(tee -ia $SDIR/bash.log)
fi

它仅适用于 bash,但是当 shell 不是 bash(即在 ksh 中)时,我会收到此错误

./col[2]: 0403-057 Syntax error at line 3 : `>' is not expected.

有什么方法可以使脚本通用吗?我基本上想创建一个脚本的输出日志文件(如果它在 bash/ksh 中运行并且也显示在屏幕上)。

尝试过

if [ "$(ps -p $$ -o comm=)" = "bash" ]; then
    exec 1> >(tee -ia $SDIR/bash.log)
fi

但不起作用。

bash ksh
1个回答
0
投票

通常使用

$BASH_VERSION
的存在来检测 bash。

if [ -n "$BASH_VERSION" ]; then
© www.soinside.com 2019 - 2024. All rights reserved.