Linux Shell expressions and linuxshell expressions
Strictly speaking, shell does not have the expression concept. Shell itself is actually a collection of a bunch of commands. Of course it is not just a collection of random together, but a certain organization. This organization is not so rigorous, so this article does not really want to summarize the so-called expressions, but to piece together some things in shell, which belong to that category ..
- Command replacement
In fact, it is the role of the anti-quotation marks in this article. However, there is another form of command substitution:
M @ meng :~ /Scripts $ m = 'date' m @ meng :~ /Scripts $ echo $ m Thursday, June 25, 2015 10:40:24 CSTm @ meng :~ /Scripts $ n = $ (date) m @ meng :~ /Scripts $ echo $ n Thursday, June 25, 2015 10:40:37 CST
That is to say, 'command' and $ (command) have the same effect.
- Arithmetic substitution
As mentioned above, when a variable is declared as an integer, it can perform arithmetic operations, or assign the number assigned to this variable to a real number instead of a string. Without this integer declaration, When a number or arithmetic expression is assigned to a variable, its value is still considered as a normal string.
But now, there is another way to achieve this effect, that is, the symbol $ (expr )). Variables in parentheses are treated as integers without being declared as integers. As follows:
m@meng:~/scripts$ r=3+4m@meng:~/scripts$ echo $r3+4m@meng:~/scripts$ echo $(($r))7m@meng:~/scripts$ echo $(($r+3))10
However, in $ (), only the +-*/and () operators can be used, and only integer operations can be performed.
- Quotation marks
The single quotation marks and double quotation marks in Shell scripts are the same as the string delimiters. The difference between the two is that all characters in single quotation marks are considered normal characters, and their functional meanings are discarded. For example, '$' is the variable value extraction character, but it is '\ $' in single quotes, as follows:
m@meng:~/scripts$ q=5m@meng:~/scripts$ echo "$q"5m@meng:~/scripts$ echo '$q'$q
Therefore, be careful when using variables in single quotes.
The so-called Delimiter is actually to treat the string in the delimiter as a whole without being interrupted by separators such as spaces.
In double quotation marks, all special characters keep their escape meanings, as shown in '$' above '. If you want a special character in double quotation marks to restore its original face, that is, take its literal value, you can add the Escape Character '\' before it, as follows:
m@meng:~/scripts$ echo "\$q"$q
- Exit status
In shell, a command can be considered as an expression. Expressions all have values. What is the value of a command? Is it the execution result? For example, what files are listed in ls? In fact, you can regard the exit status of a command as the value of a "command expression": After the command is executed, an exit status indicates whether the command is successfully executed. Generally, the exit status for successful command execution in shell is 0; failure is 1 or other non-0 values. You can use the system variable "#?". To query the export status of the previous command, such:
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 above example, the grep command does not find the character "m" in the new file, so its exit status is 1; others are 0, including the value assignment statement.
Exit is often used as the condition for determining if statements.
Test expression
Although the exit status of the command can be directly used as the judgment condition for the if and while statements, it is more often used by dedicated test commands such as test.
The syntax of the test command is:
Test expression
Test is mainly used to perform three types of tests: file attributes, value comparison, and string comparison. If the test result is true, the Exit Status of the command is 0. If the test result is false, the Exit Status of the command is 1.
File Test
Format: test options file. The main options include:
- -E tests the existence of files. If yes, the value is 0; otherwise, the value is 1. This result is consistent with the exit status obtained by running a common command.
- -D: test whether the file is a directory. The value is 0, and the value is 1.
- -F if the file is a normal file, the result is true (that is, the value is 0 ).
- -S: whether the test file is non-empty. If the file exists and is not empty, it is 0. Otherwise, the file does not exist or is empty. The value is 1.
- -R file exists and is read-only, true.
- -W file exists and can be written, true.
- -X file exists and can be executed, true.
Digital Testing
The comparison operator in the digital test uses text. The comparison is as follows (the traditional operator in C language is on the left and the shell is on the right ):
==:-Eq
! =:-Ne
>:-Gt
\ <:-Lt
>=:-Ge
\ <=:-Le
String Testing
-Z str: If the str length is 0, the result is true (remember: True <=> $? = 0)
-N str: If the str length is not 0, the result is true.
S1 = s2: The two strings are equal and the result is true.
S1! = S2: two strings are not equal and the result is true.
Str: no option is added to test str. The result is the same as-n.
[] Expressions
In fact, only [it is a command, but] It is a specifier. [The command is basically the same as the test command. The only difference is that the format is different. [the expression to be tested must be placed in parentheses, ### and leave a space between the left and right sides of the expression and [] ###, for example, [-z "empty"]. It is said that [the command is a little more efficient and I have not verified it.
Another point: ### [commands do not support the operators ">" and "<" ###, so the support for string comparison operations may be poor, this is the only difference ....
[[] Expression
The [] command is extended. The operators ">" and "<" are supported first, and logical operations are also supported ,!, As follows:
m@meng:~/scripts$ [ er > et ]m@meng:~/scripts$ echo $?0m@meng:~/scripts$ [[ er > et ]]m@meng:~/scripts$ echo $?1
Obviously, the result of er> et should be false, so the exit status of the test expression is 1. However, [] does not work, and [[] tests are correct.
M @ meng :~ /Scripts $ [er> et | er <et] bash: [: Missing '] 'er: Command m @ meng: ~ not found :~ /Scripts $ [[er> et | er <et] m @ meng :~ /Scripts $ echo $? 0 m @ meng :~ /Scripts $ [[er> et & er <et] m @ meng :~ /Scripts $ echo $? 1
- Arithmetic expression
- () Expression
It seems a bit messy. The difference between () and $ () is that the latter can extract the computation results and use them as a value. I personally feel that, '$' is used to extract a value. For example, it is also used to reference a variable. The symbol () is only used to complete the computation. As for saving the results, it is acceptable, but it is not the main business. Let's look at the command directly:
m@meng:~/scripts$ ((m=4-2))m@meng:~/scripts$ echo $m2m@meng:~/scripts$ m=$((4-2))m@meng:~/scripts$ echo $m2
This means that (m = 4-2) and m = $ (4-2) have the same effect, that is, the value can be assigned directly.
~~~~
M @ meng :~ /Scripts $ echo $ (4-2) 2 m @ meng :~ /Scripts $ echo (4-2) bash: Unexpected symbol '(' syntax error nearby
This indicates that () is not responsible for saving the results. If you want to extract the results, use '$.
~~~~
m@meng:~/scripts$ m=3;n=2m@meng:~/scripts$ echo $((m-n))1
This indicates that you do not need to add the '$' symbol when using variables in.
~~~~
m@meng:~/scripts$ m=3;n=2m@meng:~/scripts$ echo $((m<n))0m@meng:~/scripts$ echo $((m>n))1
Obviously, () also supports comparison operations. The only problem is that its exit status and other commands such as testThe opposite!
~~~~
To sum up this expression:
1. Only integer operations are supported;
2. supported operations include: = (Value assignment), = (equal), >=<=<> % +-*/-= + =/= * = % =, very rich.
- Let expression
It is basically the same as the above, but the format is different. let expressions are written after let, not in parentheses. In addition, because () does not have a natural delimiter, when there is a space in the formula, it must be enclosed in quotation marks.
- Expr expression
Write it later, so tired... The above is enough.
Logical operation
Mainly include &, |,-a,-o, and ,! And (), used to calculate the logical operation between expressions. brackets () can be used for combination,There must be spaces between logical operators and expressions..
[[] Commands only support symbols, but do not support the-a and-o options.
M @ meng :~ /Scripts $ [[er> et-o er <et] bash: syntax error in conditional expressions bash: syntax error near '-o'
.