2015-12-16 64 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兼容的。