我正在尝试创建一个基本实用程序,我需要在机器上安装 supervisord
。但问题是它取决于用户,他/她如何安装它,所以在这里我试图涵盖几乎所有场景以获得 supervisord 命令。
以下是我现在正在使用的代码。
if [[ -f "/opt/anaconda/bin/supervisord" ]]; then
RUNNER="/opt/anaconda/bin/supervisord"
elif [[ -f "/usr/local/bin/supervisord" ]]; then
RUNNER="/usr/local/bin/supervisord"
elif [[ -f "/usr/bin/supervisord" ]]; then
RUNNER="/usr/bin/supervisord"
elif [[ "$(command -v supervisord)" ]]; then
RUNNER="supervisord"
else
echo "supervisord is not install on this machine"
exit 1
fi
我正在寻找更好的方法来实现这一目标。
最佳答案
发布的代码检查了一些绝对路径,
当它找到一个存在时,
它会使用那个。
这不是一个好主意。
您可以期望用户以这样一种方式设置他们的 PATH
,以便无论在哪里,都能使用正确版本的 supervisord
。
换句话说,PATH
上可用的命令必须是首选。
if type supervisord &>/dev/null; then
RUNNER=supervisord
elif ...
...
fi
关于linux - 如何在多个命令中优先选择一个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53791022/