Problems with string conversion integers encountered while debugging shell scripts in Rhel environment

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.