Linux learning [18] bash learning in-depth 4----Judgment basis for command execution---[;, &&, ||]---used for inputting multiple instructions at one time

Preface

When using commands, after using one command, I want it to execute another command instead of waiting for command A to be executed before entering command B.
In order to achieve this effect, I consulted relevant information and compiled it here.


Judgment symbol;

After one instruction is executed and another instruction is immediately followed, you need to use the judgment symbol. ;
For example: I want to shut down the computer directly after saving the data.
instruction:source ~/.bashrc;sudo power off

This determiner is a continuous instruction issued without considering the correlation of instructions.
If the two instructions before and after are related, it needs to be completed through && or ||.

Judgment symbols && and ||

If an instruction is executed correctly, a value of $? = 0 will be returned under Linux. The value returned is the shutdown we use to determine whether subsequent instructions are executed.
How to determine whether subsequent instructions are executed &&requires||

Instruction status illustrate
cmd1&&cmd2 1. If cmd1 is completed and executed correctly ($?=0), then cmd2 starts to be executed. 2. If cmd1 is executed with an error ($?≠0), cmd2 will not be executed.
cmd1||cmd2 1If cmd1 is executed and executed correctly ($?=0), cmd2 will not be executed. 2. If cmd1 is executed with an error ($?≠0), cmd2 starts to be executed.

To give a few examples:
For example: when checking the current directory, the directory abc exists. If the abc directory exists, create the file ABC
command in the abc directory: ls abc && touch ABC
Insert image description here
Because ls abc returns wrong information at this time, the following statements will not be executed.
If we &&replace with ||, the file is created. This rule is the same as in the table above.

Summarize

With the above method, it will be much easier to enter multiple commands by yourself later. This blog post is relatively simple. It mainly focuses on the problems that came to mind during actual application. Later, I solved them by reading a book, so I will summarize them conveniently.

Guess you like

Origin blog.csdn.net/Edwinwzy/article/details/131349186