Shell Learning & | what is the difference?

Source: Internet
Author: User

Tag: Shell

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!

Shell Learning & | what is the difference?

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.