同步阅读进度,多语言翻译,过滤屏幕蓝光,评论分享,更多完整功能,更好读书体验,试试 阅读 ‧ 电子书库
Integer Conditionals
The shell also provides a set of arithmetic tests. These are different from character string comparisons like < and >, which compare lexicographic values of strings,[8] not numeric values. For example, "6" is greater than "57" lexicographically, just as "p" is greater than "ox," but of course the opposite is true when they're compared as integers.
The integer comparison operators are summarized in Table 5-3.
Table 5-3. Arithmetic test operators
Test
Comparison
-lt
Less than
-le
Less than or equal
-eq
Equal
-ge
Greater than or equal
-gt
Greater than
-ne
Not equal
You'll find these to be of the most use in the context of the integer variables we'll see in the next chapter. They're necessary if you want to combine integer tests with other types of tests within the same conditional expression.
However, the shell has a separate syntax for conditional expressions that involve integers only. It's considerably more efficient, so you should use it in preference to the arithmetic test operators listed above. Again, we'll cover the shell's integer conditionals in the next chapter.
[1] Because this is a convention and not a "law," there are exceptions. For example, diff (find differences between two files) returns 0 for "no differences," 1 for "differences found," or 2 for an error such as an invalid filename argument.
[2] The built-in command test is synonymous with [...]. For example, to test the equivalence of two strings you can either put [ string1 = string2 ] or test string1 = string2.
[3] [[...]] is not available in versions of bash prior to 2.05.
[5] NetPBM is a free, portable graphics conversion utility package. Further details can be found on the NetPBM homepage http://netpbm.sourceforge.net/
[7] Remember that the same permission flag that determines execute permission on a regular file determines search permission on a directory. This is why the -x operator checks both things depending on file type.
[8] "Lexicographic order" is really just "dictionary order."
请支持我们,让我们可以支付服务器费用。
使用微信支付打赏