字符串比较错误提示"unary operator expected"
在一个shell脚本中,原先采用了比较字符串变量
if [ $nc_id = $nc_id_1 ];then
...
else
...
fi工作正常。
但是将上述这段比较代码放到脚本的function compare() {...}段落中:
function compare() {
    if [ $nc_id = $nc_id_1 ];then
    ...
    else
    ...
    fi  
}意外发现,执行时提示错误
./check_string.sh: line 64: [: =: unary operator expected参考 unary operator expected 中有说明,如果是bash,通常使用双括号来判断 [[ ... ]] 而不是Posix兼容的单括号 [ ... ]。使用双括号 [[ ... ]] 可以不用在变量两边加上双引号,如"$nc_id":
    if [[ $nc_id == $nc_id_1 ]];then但是,如果使用Posix兼容的[ ... ],则变量两边需要使用双引号,例如:
if [ "$aug1" = "and" ];如果没有使用双引号扩起变量,则变量会被unefined或者空白,也就是变成:
if [ = "and" ];此时就会出现语法错误。
所以上述脚本改成
if [ "$nc_id" = "$nc_id_1" ];then注意:POSIX兼容的shell,在比较符号
=两边的变量都需要使用" ":
=前变量不使用引号会报错[: =: unary operator expected
=后变量不使用引号则会报错[: too many arguments
或者索性改成bash
if [[ $nc_id == $nc_id_1 ]];then参考
Last updated
Was this helpful?