Bash 如果 NOT OR 有两个命令

Bash If NOT OR With TWO Commands

我想检查两个程序是否存在

if ! [ type gedit ] || ! [ type vim ]; then
    echo "Installing programs"
    #code
fi

#code needs to run when one of them is missing.

它的真道是什么?

if ! type gedit vim &>/dev/null; then
    echo "Installing programs"
    #code
fi

测试命令 [ 的目的ose 是根据某些条件设置退出状态,例如文件是否存在或两个字符串是否相等。此处不需要,因为 type 本身设置了有用的退出状态。

另请注意,您可以指定多个名称作为 type 的参数。它 returns 只有在找到所有名称时才会出现零退出状态。

&>/dev/null os 的 purpose 将所有类型的输出,包括 stdout 和 stderr,发送到 /dev/null。如果您真的想看到它的输出,请删除该重定向。