"Shell" condition

Source: Internet
Author: User

First, there is only one type of string in the shell, no shaping or floating-point type.

However, when you define a variable, you can use single quotes, double quotes, and no quotes. Without quotes, if the variable has a white character inside it, it is truncated, so this case must be quoted, the difference between single and double quotes is variable substitution, and single quotes will not be replaced.

The syntax used as a condition test in the shell has two kinds [] and test. Generally used [] more, if in order to avoid too much [], you can use test.

Use [] to note that parentheses must have spaces before and after, otherwise the error.


Within the test block, there are only four comparison characters we can use in other languages: = =!= >, and the shell has only strings, so the four comparison characters are also for strings, which is based on the dictionary order.

In addition to the above four comparison characters, we can also use other comparison parameters, divided into strings, numbers and file three classes:

String:

if [-N str], the test is non-null and str non-null returns TRUE. This is very common, for example, we judge whether an environment variable exists.

But it's important to note that STR here must be double quotes, otherwise always true.

Or, as suggested by the author of the shell script introduction, it is only possible to put all the if of a string in double brackets [[]], that is, if [[N str]].


Digital:

There are mainly the following six kinds:

-eq

-nq

-lt

-le

-gt

-ge


File:

-F is a file or directory

-D is a directory

-E is a file


If you have more than one condition, you can use | | && connect each one []. However, it cannot appear inside [], and only command-A or-O can be used internally.

If [$a-gt] && [$a-lt];then
	echo "valid"
fi

If [$a-gt 10-a $a-lt];then
	echo "valid"
fi



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.