shell脚本报错:"[: =: unary operator expected"

发布于 分类 Linux

47天前 有1个用户阅读过

报错DEMO:

[root@localhost ~]# cat test.sh
#!/bin/bash
if [ $1 = 'noco' ] ; then
    echo 'yes'
else
    echo 'no'
fi
[root@localhost ~]# ./test.sh
./test.sh: line 3: [: =: unary operator expected
no 

解决办法:

#方法1
#!/bin/bash
if ["$1" = 'noco' ] ; then
    echo 'yes'
else
    echo 'no'
fi

#方法2
#!/bin/bash
if [[ $1 = 'noco' ]] ; then
    echo 'yes'
else
    echo 'no'
fi

-- The End --

本文标题: shell脚本报错:"[: =: unary operator expected"

本文地址: https://seonoco.com/blog/shell-script-error-unary-operator-expected

点赞 0
感谢支持
0
多谢反馈
评论 0
打赏

支持微信/支付宝

评论

网友