檢查bash手冊頁上Compound Commands
的部分。具體而言,以下幾點:
((expression))
The expression is evaluated according to the rules described below
under ARITHMETIC EVALUATION. If the value of the expression is non-zero,
the return status is 0; otherwise the return status is 1. This is exactly
equivalent to `let "expression"`.
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional
expression expression. Expressions are composed of the primaries
described below under CONDITIONAL EXPRESSIONS.
如果您正在評估需要運算,使用算術運算評估,並檢查CONDITIONAL EXPRESSIONS
節你可以用[[ ... ]]
做各種事情的東西。雙方括號中的條件可以評估字符串和整數,有時候它們的工作方式是相同的,有時候不會。
從bash的手冊頁,下CONDITIONAL EXPRESSIONS
:
string1 == string2
string1 = string2
True if the strings are equal. = should be used with the test command for POSIX conformance. When used with the [[ command, this performs pattern
matching as described above (Compound Commands).
...
arg1 OP arg2
OP is one of -eq, -ne, -lt, -le, -gt, or -ge. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than
or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers.
顯然,字符串 「1」 是字符串 「1」,所以如果n=1
,和你比較$n
對字符串"1"
,你會得到一擊。但是你應該知道你在做什麼,而這不是一個數字比較。同樣,<
和>
不是數字比較,它們是字符串比較。所以雖然"1" < "2"
,你也可能會驚訝"11" < "2"
以及。
也就是說,bash的是寬容有關什麼樣的條件,你問它來評價:
bash-4.4$ n=1
bash-4.4$ [[ n -eq 1 ]] && echo yes
yes
bash-4.4$ [[ $n -eq 1 ]] && echo yes
yes
bash-4.4$ ((n == 1)) && echo yes
yes
bash-4.4$ ((n = 2)) && echo yes
yes
bash-4.4$ echo "$n"
2
的第一個作品,因爲n
不能是什麼,但在這種情況下一個變量,所以慶典將把它因此。但是你不應該依賴這種行爲。在條件表達式中使用美元符號作爲變量,並堅持使用bash的口頭禪,「總是引用你的變量」。
在bash中的雙方括號表達式中,如果打算比較爲整數,則應使用算術二元運算符。
請注意,您的busybox版本似乎使用ash
,這不是bash
。 Ash是「Almquist shell」,是80年代後期寫成的比POSIX更早的POSIX shell。 Ash是FreeBSD中的/bin/sh
的基礎,除了在嵌入式系統(因此busybox)中比bash更受歡迎之外,由於其體積較小。
您可能需要考慮編寫POSIX shell腳本而不是Bash shell腳本。這將意味着簡化一些事情,併爲其他人跳過一些箍。 POSIX不包括雙方括號表達式,但它確實允許諸如[ "$n" -eq 1 ]
或[ $((n + 1)) -eq 2 ]
之類的東西。它會在busybox中工作。 :)
'if [[var -eq 1]];然後回顯「OK」;否則回顯「失敗」; fi'在'bash'和'ksh'上爲我打印'失敗' – anubhava
@anubhava .. echo $ var返回什麼? :) – ghoti
'declare -p var'顯示我 '-bash:declare:var:not found' – anubhava