linux shell 学习时遇到的一些问题([: 11: y: unexpected operator)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 17:07   1341   0

linux shell 学习时遇到的一些问题([: 11: y: unexpected operator)

刚刚开始接触shell 很多不懂 遇到了一些问题(linux shell 学习时遇到的一些问题([: 11: y: unexpected operator))

#!/bin/sh
#Author : iwi_ac
read -p "Please input (Y/n)" ans

if [ "$ans" == "N" ]||[ "$ans" == "n" ]; then
 echo "No,I whill go back."
elif [ "$ans" == "Y" ]||[ "$ans" == "y" ]; then
 echo "Yes!"
else
 echo "Good!" 
fi

一直在出这个错误:

[: 11: y: unexpected operator
[: 11: y: unexpected operator
[: 11: y: unexpected operator
[: 11: y: unexpected operator


改了一个小时 整个人都不好了。。

原因: Ubuntu 默认编译环境是 dash 得换用原始的bash编译 而我编译时候用的命令是 sh If.sh

最后用bash If.sh 就好了

再或者把 == 换成 = 因为dash判断的相等用 =


root@jsjx-desktop:~/Sct# bash If.sh
Please input (Y/n)y
Yes!
root@jsjx-desktop:~/Sc# bash If.sh
Please input (Y/n)n
No,I whill go back.
root@jsjx-desktop:~/Sc# bash If.sh
Please input (Y/n)w
Good!

根本上解决办法(网上借鉴):

sudo dpkg-reconfigure dash 选NO
将ubuntu 默认的shell 链接的dash 改成传统的 bash
lrwxrwxrwx 1 root root 4 8月 11 09:53 /bin/sh -> dash (为修改之前)
lrwxrwxrwx 1 root root 4 8月 11 09:53 /bin/sh -> bash
由于dash 和bash 不兼容才导致了此类问题的发生。。

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP