Ubuntu Shell脚本输出错误

时间:2015-12-16 06:14:17

标签: shell ubuntu sh scripting-language

当我运行以下代码时,我正在尝试学习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

截图:
enter image description here

3 个答案:

答案 0 :(得分:0)

Ubuntu /bin/shdash。你可以做点什么,

#!/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)

ubuntu中的

sh(1)是指向dash(1)的符号链接:

$ which sh | xargs ls -l
lrwxrwxrwx 1 root root 4 Jan 20 2015 /bin/sh -> dash

根据其手册页, ==test(1)中没有[运算符。 ==bash(1)的扩展运算符。

有两种改变方式:

  • 如果你想比较两个整数,请使用' -eq'代替
  • 如果你想比较两个字符串,请使用' ='代替。

两种方式都是posix兼容的。