当我运行以下代码时,我正在尝试学习shell脚本的基础知识我的终端返回“没有满足条件”但是当我在online shell上运行时我得到“a等于b”任何人都可以解释这个的原因。
来源:
#!/bin/sh
a=10
b=10
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "None of the condition met"
fi
答案 0 :(得分:0)
Ubuntu /bin/sh
是dash。你可以做点什么,
#!/bin/sh
a=10
b=10
if [ "$a" -eq "$b" ]
then
echo "a is equal to b"
else
echo "None of the condition met"
fi
答案 1 :(得分:0)
==
运算符不可移植,显然您安装为/bin/sh
的shell使用的内置版本[
无法识别该运算符。只需将其替换为=
。
答案 2 :(得分:0)
sh(1)
是指向dash(1)
的符号链接:
$ which sh | xargs ls -l
lrwxrwxrwx 1 root root 4 Jan 20 2015 /bin/sh -> dash
根据其手册页,
==
或test(1)
中没有[
运算符。 ==
是bash(1)
的扩展运算符。
有两种改变方式:
两种方式都是posix兼容的。