2015-12-16 34 views
0

我试图学习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 

截图:
enter image description here

回答

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 
0

==运营商是不可移植的,显然你已经安装为/bin/sh外壳使用的[一个内建的版本,不承认运营商。只需将其替换为=即可。

0

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兼容的。