我的 .bashrc 中有一个部分可以启动 tmux 程序(终端多路复用器)。
但是,如果未安装 tmux 程序(比如我正在设置一台新计算机),那么在我的 .bashrc 文件中使用它会阻止任何终端窗口成功打开。
当然安装 tmux 可以解决这个问题,但这不是我的问题。
我怎样才能使这个有条件,这样如果没有安装tmux,它就不会出错/给出错误消息?
目前我有:
if [[ ! $TERM =~ screen ]]; then
exec tmux
fi
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
if tmux; then
if [[ ! $TERM =~ screen ]]; then
exec tmux
fi
fi
Run Code Online (Sandbox Code Playgroud)
但这给了我
The program 'tmux' is currently not installed. You can install it by typing:
sudo apt-get install tmux
Run Code Online (Sandbox Code Playgroud)
虽然它至少给了我一个终端提示而不是关闭窗口!此外,如果/安装 tmux 时,这不会在打开新终端窗口时出现任何错误/导致任何问题。
您可以使用该命令type
查看您的机器上是否存在可执行文件:
if [ -n "$(type -P tmux)" ]; then
...tmux is installed...
else
...tmux isn't installed...
fi
Run Code Online (Sandbox Code Playgroud)
我经常使用这个代码片段来做到这一点:
$ [ -n $(type -P tmux) ] && echo "installed" || echo "not installed"
installed
Run Code Online (Sandbox Code Playgroud)
我可以使用替代-n
(非空字符串),-z
(空字符串)来伪造它。
$ [ -z $(type -P tmux) ] && echo "installed" || echo "not installed"
not installed
Run Code Online (Sandbox Code Playgroud)