Shell Learning & amp; and | what is the difference?
Before answering the questions in this chapter, let's first understand the concept of return value!
Each command or function we run in shell will return a value of the parent itinerary at the end,
Return value.
Available in shell command line $? This variable gets the most "new" return value, that is, the final
The value returned by the stroke of the bundle.
The Return Value (RV) is between 0 and 255, which is determined by the author of the Program (or script:
* In script, exit RV is used to specify the value. If exit RV is not specified, the RV value of the last command is used as the end time.
* If it is in the function, use return RV instead of exit RV.
Return Value is used to determine the exit status of a trip. There are only two types:
* 0 indicates "true" (true)
* If the value is not 0, it is "false" (false)
The following is an example:
Assume that there is a file my. file in the current directory, and no. file does not exist:
Code: $ touch my. file
$ Ls my. file
$ Echo $?
# First echo
0
$ Ls no. file
Ls: no. file: No such file or directory
$ Echo $?
# Second echo
1
$ Echo $?
# Third echo
0
The first echo in the above example is about the RV of ls my. file. The value of 0 can be obtained, so it is true;
The second echo is about the RV of ls no. file. A non-0 value is obtained, so it is false;
The third echo is about the second echo $? Is 0, so it is true.
However, there is a command dedicated to test a condition and send return value for true or false
Judgment,
It is the test command!
If you are using bash, enter man test or man bash in command line to learn about this test.
.
This is the most accurate file you can use for reference. If you listen to what others say, just for reference...
Below I will only provide some auxiliary instructions, and the rest will be subject to man:
First, the expression of test is called expression. There are two command formats:
Code:
Test expression
Or:
[Expression]
(Note the Space key !)
The same effect is applied to any format. (I personally prefer the latter ...)
Second, bash test currently supports only three test objects:
* String: string, that is, plain text.
* Integer: integer (0 or positive integer, excluding negative or decimal points ).
* File: file.
Please be sure to figure out the differences between the three, because the expressions used by test are different.
Take A = 123 as an example:
* ["$ A" = 123]: it is A string test to test whether $ A is 1, 2, or 3 consecutive "text ".
* ["$ A"-eq 123]: an integer test to test whether $ A is equal to "One hundred and twenty-three ".
* [-E "$ A"]: A test on the file to test whether the 123 "file" exists.
* If you encounter variable replacement in test, using soft quote is the safest!