我試圖學習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兼容的。