不能' sudo'在bash if语句中

时间:2014-05-20 16:09:02

标签: linux bash

我是双Linux启动我是bash的新手

运行以下脚本时出现了奇怪的错误:

if [[ 'grep -i fedora /etc/issue' ]]; then
        echo "the OS is Fedora"
        $(sudo yum update -y && sudo yum upgrade -y)
else
        echo "the OS is Ubuntu"
        $(sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y)
fi
  

错误:./ server_update.sh:第9行:已加载:未找到命令

1 个答案:

答案 0 :(得分:2)

尝试执行apt-get / yum命令的输出,丢失$(..)

您在开始时也遇到问题:

if [[ -n "$(grep -i fedora /etc/issue)" ]]; then

是检查字符串是否存在的正确方法。

您的代码应如下所示:

if [[ -n "$(grep -i fedora /etc/issue)" ]]; then
    echo "the OS is Fedora"
    sudo yum update -y && sudo yum upgrade -y
else
    echo "the OS is Ubuntu"
    sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y
fi