检查bash中的布尔表达式

时间:2016-06-16 13:29:53

标签: bash

我遇到以下检查布尔变量的麻烦:

for i in "${list[@]}"
do
        if [ $i = "web.war" ] ; then
        $deployed = true; echo "Deployed!!!!!";
        fi
done

echo $deployed;

在这个例子中,条件$ i =" web.war"实际上是文本" 已部署"打印出来。但是,当我在最后一行打印时,变量$ deployed的值为false。是变量范围的问题还是什么? 感谢

1 个答案:

答案 0 :(得分:3)

$deployed = true

对于作业,请删除$和空格。

deployed=true

Shellcheck.net是一个很棒的网站,用于检查shell脚本的语法错误,常见错误,拼写错误等。例如,这是为您的代码段打印的内容。

Line 1:
for i in "${list[@]}"
          ^-- SC2154: list is referenced but not assigned.

Line 3:
        if [ $i = "web.war" ] ; then
             ^-- SC2086: Double quote to prevent globbing and word splitting.

Line 4:
        $deployed = true; echo "Deployed!!!!!";
         ^-- SC1066: Don't use $ on the left side of assignments.
                  ^-- SC1068: Don't put spaces around the = in assignments.