The logical relationship of the multiple command execution of bash features:
1. Command replacement
COMMAND1 $ (COMMAND2)
2. Piping
COMMAND1 | COMMAND2 | COMMAND3 ...
3. Sequential execution structure
COMMAND1; COMMAND2; COMMAND3 ...
4. Select the execution structure:
If... So...
Either... Either...
Logical operation:
With: Logical multiplication, 0--false, &&, binocular operator
1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0
Short-circuit algorithms for "and" operations: if the first operand is false, the result of its logical operation must be false; or: logical addition, 0--, False, | |, binocular operator 1 | | 1 = 1 1 | | 0 = 1 0 | | 1 = 1 0 | | Short-circuit algorithm for 0 = 0 "or" Operation: as long as the first operand is true, the result of its logical operation must be true; Non: logical negation, 0--false,!, monocular operator!0 = 1! 1 = 0 Execution status return value of the command: Success:0--ture (True) failure:1-255--false (FALSE) COMMAND1 && COMMAND2 1. If COMMAND1 can execute successfully, COMMAND2 will be executed; 2. If COMMAND1 execution fails, COMMAND2 will not be executed; COMMAND1 | | COMMAND2 said: 1. Only COMMAND1 execution fails, COMMAND2 will be executed; 2. If COMMAND1 execution succeeds, then COMMAND2 will not be executed;! COMMADN1 && COMMAND2 equivalent to COMMAND1 | | COMMAND2! COMMAND1 | | COMMAND2 equivalent to COMMAND1 && COMMAND2 De Morgan Law! (COMMAND1 && COMMAND2) | | COMMAND3! (COMMAND1 | | COMMAND2) && COMMAND3! A && B = a | | B! A | | b = A && b! (A && B) =! A | | ! B! (A | | B) =!A &&! B Three Logical operations priority:!>&&>| | Example: If the user user4 exists and its home directory also exists, then the Userdel-r user4 command is executed; ID user4 && ls-d/home/user4 && userdel-r user4
Bash script Programming:
What is programming?
The process of writing program source code using human natural language or machine language.
Why do you want to program? In order to enable the user to use the computer, you can let the computer do some tasks in a non-interactive way, at this time, users need to edit such tasks as a file, and let the computer follow a specific book order to read the task, so as to achieve the desired function In order for the computer to interpret the contents of such a file and execute it correctly, the program source code file must be converted to a binary format that the computer can directly recognize and use, which is called compilation, and a specific compiler tool must be used to complete the compilation process; No matter what programming language you use to write programs, you must strictly follow the specific format and syntax structure that the compiler can recognize. Programming languages: Advanced languages: Classification according to source code: compiled language: source code --Compiler (compile)--[Linker (link)--] Assembler (assembler)--Executable binary code file; Explanatory language source code-interpreter (row by line explanation) --Edge interpretation edge execution depending on whether the functional implementation in the programming process is calling the library or calling the external program file classification: Full programming language: Programming with libraries or programming components; scripting languages: Using an interpreter to invoke the selected external application; classification according to the program's writing specification: Procedural language: program = instruction + data centered on instructions, around instructions The function realizes the design data and the data structure, the information is the instruction service, the algorithm and the instruction realization form: Sequential execution chooses executes the loop execution Object-oriented language: program = algorithm + data structure is the center of information and structure, the data is instantiated, around the requirements of the data to deploy the algorithm; Class (Cla SS): instantiated data; attribute (Attibution): The basis of distinguishing different objects in the same class; Method: The correct operation method of the class; low-level language: assembly language: Machine language: Binary language shell Scripting--bash scripting: Procedural programming languages, interpreting running programming languages, scripting languages (depending on the external application file shipped Line) What exactly is a shell script? 1. Plain text document-all the stored or contained instruction + data in a file is stored in characters; 2. A simple or complex command combination that solves a user's problem according to the user's needs; 3. is a program entity with "power of execution"; execution idempotent: The result of one execution of any command and Multiple execution results are consistent; Note: Many commands do not have "idempotent", so there is a need to use a lot of program logic in the shell script to determine whether a command conforms to its operating conditions, thereby avoiding critical errors that occur during the run, and how the code in the shell script Writing? 1. The first line must be shebang, that is: the absolute path of the interpreter program, must occupy the absolute beginning of the line and must occupy the first row, in the execution of the script, according to the instructions of shebang, the corresponding interpreter is started to explain the script is a lot of commands; #!/bin/bash #!/bin/ SH #!/usr/bin/python #!/usr/bin/perl ... 2. In the shell script, all lines with the # character line at the beginning are interpreted as comment lines except for shebang, that is, the interpreter interprets its contents but does not execute it; 3. The interpreter ignores all blank lines in the script file; a blank line refers to: in a line of text, in addition to white space characters, the space character , a line other than a tab character that does not have any other type of character; 4. A large number of command and keyword commands: internal or external application keywords: commands built into the shell that can only be executed in a particular struct; keyword; if, El SE, then, does, while, for, select, until, Case, FI, Esac,... All special function characters in 5.shell; Note: All commands, keywords, and symbols written into the shell script document must be ASCII encoded characters, and other encoded characters can appear in the shell script file, but have no special meaning; how do I write a shell script? can facilitateShell scripting with all text document editing tools such as: Nano, VI, Vim, Pico, emacs, ... It is usually recommended to use VIM in various distributions of Linux, and script file naming: In general, the name suffix of ". Sh" will be set for the script, and the lower version of the editing tool will identify the shell script file based on the suffix name of the file; A high-level version of the text Editing tool, such as Vim7 , there is no problem with too many relational file suffix names. How the script works: 1. Give execute permission to the script file, you can run the file directly as an absolute or relative path; # chmod +x/path/to/some_script_file #/path/to/some_scrip T_file Note: If the directory path where the script file resides is stored in the path variable, it is executed directly with the script file name; 2. Run the script directly with the interpreter, using the script file as the parameter of the interpreter program; # BASH/PATH/TO/SOME_SCRI Common options for the Pt_file Bash command:-x: To make bash appear on standard output in interpreting the script, typically to debug the shell script-N: Pre-execute the script file to parse the script There is a syntax class error; If there is no error, no information is output; In contrast, a concise hint is printed; Note: In this way, whether the script file has Execute permission is not an important property; Note: In both of these ways, when executing a script, the current shell A new child shell is opened in order to run the script; Normally, when the script is finished, the child shell is destroyed as well, so all variables defined in the script, at the end of the script, should be explicitly revoked; 3. Run the script with the source command; # SOURCE/PA Th/to/some_script_file #. /path/to/some_script_file Note: The 1.source command does not open a child shell while running the script, but rather runs in the current shell; 2. Do not package in scripts executed with the source command Including commands such as the Exit class;
Bash features and shell scripting introduction