string-to-integer type! This requirement should be common in programming languages, and it's too cumbersome in the shell. Someone asked why the conversion? The problem I encountered is that the number of parameters is $ #需要进行整型运算, while $ #取出来的却是字符串, the following is the certificate:
if [$#! = 1]; Then
......
Fi
This can only be used with the!=,=,== string of the comparator, and the use of-EQ,-LT these integer comparator error. Need to be used to do the operation must be converted to integral type, on-line search and then asked a large circle of friends, the method is to get this
i= ' expr $# + 0 '
This method is a bit funny, but it seems that there is no other better way to do it!
Also note that the IF condition is an integer comparison when you must use both brackets, for example:
i= ' expr $# + 0 '
j= ' expr $i + 1 '
if [[I-lt J]]; Then
......
Fi
The string comparison uses a single bracket, such as the first example above.
Problems with string conversion integers encountered while debugging shell scripts in Rhel environment