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 不兼容才导致了此类问题的发生。。
|