【问题标题】:bash update script returns true but fails anyway [duplicate]bash更新脚本返回true但无论如何都会失败[重复]
【发布时间】:2016-07-04 17:08:56
【问题描述】:

我正在编写“启动更新”bash 脚本,但在 Konsole 中打开它会返回:

~~~~~~-~~~~~@~~~~~~-~~~~~-~~-~~~~~~~~-~~~:~/Documents$ ./update.sh
./update.sh: 3: ./update.sh: [True=True]: not found
~~~~~~-~~~~~@~~~~~~-~~~~~-~~-~~~~~~~~-~~~:~/Documents$

这里是代码(screenshot):

#!/bin.sh
upd=True
while [$upd=True]
do
    echo "Updateing..."
    end=0
    sudo apt-get -yV update
    if [$?=0 -o $end=0]
    then
        end=1
        sudo apt-get -yV upgrade
        if [$?=0 -o $end=1]
        then
            end=2
            sudo apt-get -yV autoremove
            if [$?=0 -o $end=2]
            then
                end=3
                sudo apt-get -yV autoclean
                if [$?=0 -o $end=3]
                then
                    end=4
                    sudo apt-get -yV dist-upgrade
                    if [$?=0 -o $end=4]
                    then
                        end=5
                        sudo apt -yV update
                        if [$?=0 -o $end=5]
                        then
                            end=6
                            sudo apt full-upgrade
                            if [$?=0 -o $end=6]
                            then
                                upd=False
                            fi
                        fi
                    fi
                fi
            fi
        fi
    fi
done
exit 0

【问题讨论】:

  • 您应该将代码作为文本发布在问题中。
  • 同意 \@SurvivalMachine 所说的。
  • 在此处发布代码之前学习使用shellcheck.net并且不要发布屏幕截图! ;-) 祝你好运。
  • 您应该使用if [[ "$upd"="True" ]](引号以防止出现空变量问题)
  • @shellter 谢谢!它(shellcheck.net)帮助很大,但仍然要求 sudo 输入密码,如果您或其他人知道如何处理,那就太好了。这个想法是在启动时更新而不必处理 sudo

标签: bash shell sh


【解决方案1】:

我想问题出在

while [$upd=True] #Mind the spaces after [ and before ]

应该是的

while [ "$upd" = True ] #Also the mind spaces before and after =

注意

$upd 放在双引号中以防止分词。

【讨论】:

  • = 周围还需要空格:while [ "$upd" = True ]
  • @WilliamPursell :发布此内容时有点困。更新:)
  • @sjsam > 第 2 行同样的错误 + sudo 提示和 inf 循环,新代码 (code)
猜你喜欢
  • 2018-11-29
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2016-03-07
  • 2014-03-13
  • 2013-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多