Bash features multiple command executions, such as: # COMMAND1 $ (COMMAND2), and communication between Processes (IPC): # COMMAND1 | COMMAND2 ...
I. Command execution structure and algorithm
In command execution, there are sequential execution structures: semicolon delimited # COMMAND1; COMMAND2; ...。 There are also selection execution structures: logical operations and, or, non-, XOR, the most important of which is the selection of the execution structure, specifically discussed below.
1, with: logical multiplication &&
Status return value: 0 corresponds to true,1-255 corresponding to False
The specific algorithm is: true &&true = = True
True && false = = False
False &&true = = False
False && false = = False
There is a short-circuit logic operation with an operation: As long as the first logical operand is false, the result of its logical operation must be false. For example, COMMAND1 && COMMAND2, interpreted as: If COMMAND1 can execute successfully, COMMAND2 will be executed, and if COMMAND1 can execute successfully, COMMAND2 will be executed
2, or: logical addition | |
The exact algorithm is: True | | True = = True
True | | false = = True
False | | True = = True
False | | False = = False
Or short-circuit logic operation: There is a logical operand of true, then its logical operation result must be true. For example COMMAND1 | | COMMAND2, interpreted as: If the COMMAND1 can execute successfully, then COMMAND2 is not necessary to execute; If COMMAND1 execution fails, COMMAND2 will not be executed
3, non: logical take the reverse!
The specific algorithm is:! COMMAND (! true = False,! False = True)
Combined with the above three points, you can get:
Logical operator Precedence:! > && > | |
! COMMAND1 &&command2 equivalent to COMMAND1 | | COMMAND2, interpreted as: If the COMMAND1 can execute successfully, then COMMAND2 is not necessary to execute; If COMMAND1 execution fails, COMMAND2 will not be executed
! COMMAND1 | | COMMAND2 equivalent to COMMAND1 && COMMAND2, interpreted as: COMMAND2 will be executed if COMMAND1 succeeds, COMMAND1 will not be executed if COMMAND2 execution fails
Demogan Law:! (a&&b) ==! a| |! B
! (a| | B) ==! a&&! B
Examples of Demogan law are:! (COMMAND1 &&command2) | | COMMAND3, interpreted as: COMMAND1 and COMMAND2 execute successfully at the same time COMMAND3
Implementation of an algorithm or instruction: Sequential execution, selection execution, loop execution
4, XOR or
is different than the two operands. If different, the result of the logical operation is true, if the same, the result of the logical operation is False
Second, Shell script programming
The programming of shell script programming is to write program source code. In order to enable users to do some tasks in a non-interactive way while using the computer, we need to make these tasks into a file that allows the computer to read sequentially, thus enabling the function to be implemented by the dynamic compilation of shell scripts.
A shell script is a large number of command combinations that interpret user issues based on user needs, are plain text documents, invoke shell programs, and second call the required external command file, the command interpreter. Shell scripting is a process-based programming language that interprets running, relies on external program files to run a one-way interface that provides a single function, over-programming logic support.
Many commands do not have "idempotent", and in shell scripts it is necessary to use a lot of program logic to determine whether a particular command satisfies its operating conditions to avoid serious errors during operation.
Programming languages are divided into high-level and low-level languages, as explained below.
1. Advanced language
(1), according to the source code processing method is divided into: Compile the running language, explain the running language
The specific process for compiling a running language is: source code-----> compiler (Assembler)-----> [Connector (link behavior to complete library)]-------> binary files that can be executed directly
Explain the process of running the language as follows: source code-------> Direct Start interpreter program corresponding to source code, interpreted by interpreter side Execution
(2), according to the implementation of the function in the programming process is the call library or call external programs are divided into: Complete programming language, script programming language
Full programming language: Programming with libraries or programming components
Script programming Language: Interpreter programming
(3), according to the programming paradigm of the program is divided into: Process programming language, object-type programming language
Programming languages: Design data and data structures around the instruction (algorithm), the command service
The object-oriented programming language is: the data (structure) as the center, the data is instantiated into a class, around the requirements of the data to deploy instruction classes, properties, methods
2, low-level languages: assembly language.
The code content in the shell script has the following requirements and explanations:
1, the first line must be shebang, the interpreter path, must occupy the absolute beginning; when executing, start the appropriate interpreter to explain the many commands in the script
For example: #!/bin/bash; #!/bin/sh; #!/usr/bin/python
2, in the shell script, in addition to Shebang, to # occupy the absolute beginning of the content, are comment lines, the interpreter will ignore such a line of content
3. Explanation ignores all blank lines in the script
4. A large number of commands and keywords (if, else, then, does, while, for ...) )
Note: Once the shell script is running, it is in the current shell to open an indicator (child shell) explaining the execution of the code according to Shebang's instructions, and the shell script content is implemented in a child shell.
The methods of running the script are as follows:
1, to give the text file execution permissions, run the file directly, if the script is executed, the script file name is not given the path, you must ensure that the path variable in the saved paths, you can find the text file
Example: chmod +x/path/to/script_file
~]#/path/to/script_file
2. Run the script directly with the interpreter, using the script as the parameter of the interpreter command
Example: Bash/path/to/script_file
Fundamentals of algorithm and Shell scripting programming