`if test x $ {property} = x;`一个赋值或比较?

时间:2014-11-10 09:00:43

标签: bash

请让我理解这段代码。

bash shell脚本中是否发生了分配或比较?

if test x${property} = x; then
some condition

fi

它永远不会进入这个if块

2 个答案:

答案 0 :(得分:1)

实际上,您正在检查变量是否为空。如果该变量是emplty,则执行该块。

您可以看到以下说明。

> echo ${property}

> if test x${property} = x; then echo variable is empty;fi
variable is empty
> property='some data'
> if test "x${property}" = x; then echo variable is empty;fi

您也可以使用-z选项进行检查。

if [ -z "${property}" ]; then 
    echo "variable is empty"
else
    echo "variable is not empty"
fi

答案 1 :(得分:0)

这只是在旧炮弹中进行比较的一种方式,它没有很多选择。它测试property是否为空。在现代shell中,例如kshbash,您将使用-z,等效条件为:

if [[ -z ${property} ]]; then
  some condition
fi