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

发布于 2015-12-28 作者 [重庆SEO]

报错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