ubuntu10.04 shell编程 if-else条件判定出现unexpected operator错误

来源:互联网 发布:centos中文 编辑:程序博客网 时间:2024/05/18 09:11

http://nubnub.blog.163.com/blog/static/169186347201191591835616/

 

read -p "Please input (Y/N): " yn

if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
 echo "OK,continue"
 exit 0
elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
 echo "Oh,interrupt"
 exit 0
else
 echo "I don't know what your choice is"
 exit 0
fi

 

输入Y或N出现如下信息

[: 19: Y: unexpected operator
[: 19: Y: unexpected operator
[: 19: Y: unexpected operator
[: 19: Y: unexpected operator

因为ubuntu默认的sh是连接到dash的,又因为dash跟bash的不兼容所以出错了,执行时可以把sh换成bash 文件名.sh来执行,成功。

        修改sh默认连接到bash的一种方法:

        sudo dpkg-reconfigure dash

选择no即可.第二种:
       在某个论坛看到别人讨论的结果。

将== 改为=就行了,dash判断字符串相等用 的是=。

原创粉丝点击