我试图学习shell脚本的基本知识,当我运行下面的代码时,我的终端返回“没有遇到任何条件”,但是当我在online shell上运行它时,我得到“a等于b“任何人都可以解释这个原因。Ubuntu Shell脚本给出错误的输出
来源:
#!/bin/sh
a=10
b=10
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "None of the condition met"
fi
我试图学习shell脚本的基本知识,当我运行下面的代码时,我的终端返回“没有遇到任何条件”,但是当我在online shell上运行它时,我得到“a等于b“任何人都可以解释这个原因。Ubuntu Shell脚本给出错误的输出
来源:
#!/bin/sh
a=10
b=10
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "None of the condition met"
fi
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
的==
运营商是不可移植的,显然你已经安装为/bin/sh
外壳使用的[
一个内建的版本,不承认运营商。只需将其替换为=
即可。
sh(1)
在Ubuntu是一个符号链接dash(1)
:根据其手册页
$ which sh | xargs ls -l
lrwxrwxrwx 1 root root 4 Jan 20 2015 /bin/sh -> dash
, 有一个在test(1)
或[
没有==
运营商。 ==
是bash(1)
的扩展运算符。
有两种方法来改变:
而且两种方式都是POSIX兼容的。