The expression __linux of Linux shell

Source: Internet
Author: User
Tags arithmetic logical operators

Strictly speaking, there is no concept of an expression in the shell. The shell itself is simply a collection of commands, and certainly not a bunch of random piles, but a certain organization. But this organization is not so rigorous, so this article is not to really summarize the so-called expression, but the shell of some of the things in the corner of the pieces together, really do not know they belong to that classification. Command substitution
is actually the effect of the inverted quotes in this article. But there is another form of command substitution, as follows:

m@meng:~/scripts$ m= ' Date '
m@meng:~/scripts$ echo $m
Thursday, June 25, 2015 10:40:24 CST
m@meng:~/scripts$ n=$ ( Date)
m@meng:~/scripts$ echo $n
June 25, 2015 Thursday 10:40:37 CST

In other words, the ' command ' and the $ (command) effect are the same. Arithmetic substitution
As I said before, when you declare a variable as an integer type, it can do arithmetic, or the number assigned to the variable is no longer a string, but a real number. Without this integer declaration, a numeric or arithmetic expression is assigned to a variable, and its value is still treated as a normal string.
But now there is another way to do this, which is the symbol $ (expr). A variable in parentheses is treated directly as an integer without having to declare it to be an integer type in advance. As follows:

m@meng:~/scripts$ r=3+4
m@meng:~/scripts$ echo $r
3+4
m@meng:~/scripts$ echo $ (($r))
7
m@meng:~ /scripts$ echo $ (($r +3)) #
10

However, only the +-*/and () operators can be used in $ (()) and only integer operations can be done. Quotes
The single and double quotes in the shell script are the same as the strings, and the difference is that all the characters in single quotes are treated as normal characters and discard their functional meanings. For example, ' $ ' is a variable value extractor, but in single quotes it is the literal value of ' \$ ', as follows:

m@meng:~/scripts$ q=5
m@meng:~/scripts$ echo "$q"
5
m@meng:~/scripts$ echo ' $q '
$q

Therefore, you must be careful to use variables in single quotes.
The so-called definition character, in fact, is to treat the string within the definition as a whole, without being interrupted by whitespace and other delimiters.
Within double quotes, all special characters retain their escape meanings, such as the ' $ ' above. If you want a special character in a double quote to revert to its true value, you can add the escape character ' \ ' before it, as follows:

m@meng:~/scripts$ echo "\ $q"
$q
Exit status
In a shell, you can think of a command as an expression. Expressions are all of a value, and what is the value of a command? Is it the result of its execution. such as LS column out of the file. In fact, you can treat the export status of a command as a value of a command expression: After the command completes, there is an exit state that indicates whether the command was executed successfully, and in general, the command execution in the shell successfully exits at 0; failure is 1 or other non-0 values. You can use the system variable "#?" To query the exit status of the previous command, such as:
m@meng:~/scripts$ grep m new 
m@meng:~/scripts$ echo $?
1
m@meng:~/scripts$ cat new
m@meng:~/scripts$ echo $?
0
m@meng:~/scripts$ m=4
m@meng:~/scripts$ echo $?
0

In the previous example, the grep command did not find the character "M" in the new file, so its export status was 1; others were 0, including assignment statements.
The exit state is often used as a criterion for an if statement.

Test expression
Although it is possible to use the export status of the command as a criterion for the IF, while, and so on, it is more commonly used in special test commands such as test.
The syntax for the test command is:
Test expression
Test is primarily used to perform three types of tests: file properties, numeric comparisons, string comparisons. If the test result is true, the command's exit status is 0, and if the test result is false, the command's exit status is 1.

File test
The format is: Test options file. The main options include the existence of the-e test file, with a value of 0 if present, or 1. This result is consistent with the export status of the execution of ordinary orders. -D test file is a directory, is 0; no, 1. -F If file is normal, the result is true (that is, the value is 0). -S test file is not NULL, if file exists and is not NULL, is 0, otherwise, that is, does not exist or is an empty file, 1. -R file exists and is read-only, true. The-W file exists and is writable, true. -X file exists and can be executed, true.

Digital test
The comparison operator for a numeric test actually uses text, as contrasted with the following (the traditional operator used by the C language on the left is the shell used on the right):
= =:-eq
!=:-ne
>:-GT
\<:-lt
>=:-ge
\<=:-le

String test
-Z Str: If the length of STR is 0, the result is true (remember: true <==> $?) = = 0)
-N STR: If the length of STR is not 0, the result is true
S1 = s2: Two strings equal, the result is true
S1!= S2: Two strings Not equal, the result is true
STR: Test str without any options, same effect as-n

[] An expression
In fact, only [is a command, and] just a qualifier. The command is essentially the same as the test command, and the only difference is that the format is different, [the command requires the expression to be tested in parentheses, # # #且表达式左右两边分别要和 [] Leave a space ###, such as [-Z ' empty]. It is said that [the efficiency of the order is slightly higher, I have not verified it.
One more thing: The ###[command does not support operator ">" and "<" ###, so support for string comparison operations may be almost, this is the only two difference ....

[[]] An expression
extended the [] command, which first supported the operator ">" and "<", and also supported logical operations &&,| |,!, as follows:

m@meng:~/scripts$ [er > et]
m@meng:~/scripts$ echo $?
0
m@meng:~/scripts$ [[er > et]]
m@meng:~/scripts$ echo $?
1

Obviously, er > et results should be false, so the exit status of the test expression is 1. But [] there is no effect, and [[]] the test is correct.

m@meng:~/scripts$ [er > et | | er < ET]
bash: [: Missing '] '
er: command not found
m@meng:~/scripts$ [[er > et | | er < ET]]
m@meng:~/scripts$ echo $?
0
m@meng:~/scripts$ [[er > et && er < et]]
m@meng:~/scripts$ echo $?
1
An arithmetic expression
(()) An expression
It seems a bit messy, (()) and $ (()) The difference is that the latter can be extracted from the calculation as a value, I personally feel, ' $ ' the symbol is used to extract the value, such as reference to the variable when the use of it. and the symbol (()) Just completes the calculation itself, as to save the result, although it can, but is not the main business. Just look at the order:
m@meng:~/scripts$ ((m=4-2))
m@meng:~/scripts$ echo $m
2
m@meng:~/scripts$ m=$ ((4-2))
m@meng:~/ scripts$ Echo $m
2

This means that ((m=4-2)) and m=$ ((4-2)) effects are the same, meaning that the assignment can be done directly within (()).
~~~~

m@meng:~/scripts$ echo $ ((4-2))
2
m@meng:~/scripts$ Echo ((4-2))
bash: unexpected symbol ' (' nearby syntax error

This means that (()) is not responsible for saving the results, if you want the results, use ' $ ' to extract.
~~~~

m@meng:~/scripts$ m=3;n=2
m@meng:~/scripts$ echo $ ((m-n))
1

This means that you do not need to add the ' $ ' symbol when using variables internally (()).
~~~~

m@meng:~/scripts$ m=3;n=2
m@meng:~/scripts$ echo $ ((m<n))
0
m@meng:~/scripts$ echo $ ((m>n))
1

Obviously, (()) also supports comparison operations, and the only problem is that its export status is the opposite of other commands, such as Test .
~~~~
Summarize this expression:
1, only support integer operation;
2, supported operations include: = (Assignment), = = (equal), >= <= < >% +-*/= = +/= *=%=, very rich. Let-expression
Basically the same as the above, but the format is different, let the expression written in the let behind, not parentheses. In addition, because unlike (()) have a natural definition, there are spaces in the formula that need to be enclosed in quotes. Expr expression
Write it later, so tired ... The above is enough.

Logical operations
There are &&, | |,-A,-O,!, and (), which are used to compute logical operations between expressions, support the combination of parentheses (), and have spaces between logical operators and expressions .
The [[]] command supports only symbols, and the-A and-o option is not supported.

m@meng:~/scripts$ [[er > Et-o er < et]]
bash: syntax error in conditional expression
bash: syntax error near ' o '

Finish.

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.