Shell Programming Basics: logical Operations

Source: Internet
Author: User

Recently around here for a long time ah, now reluctantly come out, share a little understanding of their own

Exit status

A program once run must have only two kinds of results, either success or failure, the result of success is the same, the reason for failure is different (click \~~\~, re). Success and failure procedures are all to exit, and successful returns a successful message to the system, and the failure will tell the system to fail itself. This state of success or failure is the exit state.

Exit Status Code

The program returns a number to the system on exit to represent its exit status, which is called exit status code.
Exit status code stored in '? ' Variable, you can use the echo $? view
Exit [n] can be used in the script to specify the status code when the script exits (Note: Exit causes the script to exit immediately, at which point the exit status code is specified by exit.) If exit final status code is not specified depends on last command)

Condition test

A condition test is used to determine whether a requirement is satisfied. If two numbers are equal, two strings are the same ...
The format of the conditional test is this:
Test EXPRESSION
[EXPRESSION]
[[EXPRESSION]]
The condition test is still returning a value to represent the test result,

Logical operations

With: false must be false
Or: There is truth to be true
Non: TRUE or false is true

command1 && command2

Logic and. Command2 is executed when Command1 is executed correctly, Command2 is not performed if the Command1 is not executed correctly. In bash, pass a predefined variable "$?" To determine if the command is executed correctly, if "$?" A value of 0 indicates that the previous command was executed correctly, and any other value indicates an incorrect execution.

[[email protected] ~]$·echo hello && ls hellohellols: cannot access hello: No such file or directory[[email protected] ~]$·ls hello && echo hellols: cannot access hello: No such file or directory

Why, take a look at the logic and, there is false must be false, that is, when Command1 is false this expression Command1&&command2 has determined that the result is false, Command2 also does not have to be executed (because at this time both true and false expressions are false Command2)

command1 || command2

Logical OR, only when Command1 is false will it execute Command2

[[email protected] ~]$·ls hello || echo hellols: cannot access hello: No such file or directoryhello[[email protected] ~]$·echo hello || ls hellohello

is still above, logical or TRUE, that is, when Command1 is true this expression Command1 | | Command2 is already true command2 it does not have to be executed (has no effect on the result)

!!!! Very IMPORTANT!!!
Don't pay too much attention to 01, only care about true
Don't pay too much attention to 01, only care about true
Don't pay too much attention to 01, only care about true

Why has not said the return value question, because this is a pit, I in inside almost cannot get out. First of all, my conclusion:
The return value of the command (exit status code, condition test is also a command) represents the program's exit status, not true or false

Well, the following is particularly round.
First, True and false corresponding 1 and 0
Second, the command execution success is true, the failure is false
Third, the return value corresponds to 0 or not 0 (conditional tests return only 0 and 1)
Four, the return value of 0 and not 0 corresponds to the program's exit status, 0 means that the execution succeeds, not 0 means failure
In the logic operation, the return value is used to judge the true and False

such as Command1 | | Command2

Command1 execution → return value
The above Command1 has ended
query return value → from return worth true and false → start logical operation

The conditional test is easier to understand because the return value is only 0 and 1 more confusing, as long as you can accept that conditional testing is a command setting.

command1&&command2| | Command3
It's easy to understand what Command3 was like before, as a command.

Shell Programming Basics: logical Operations

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.