Programming Basics:
Program: Instruction + data
Program Programming Style:
Program: command-centric, data-serving instruction
Object type: Data-centric, instruction serves data
Shell Program: Provides the programming capability to interpret the execution. belongs to the object type. The interpreter is translated into binary instructions that the computer can run.
Programming logic processing mode: Sequential execution, loop execution, select execution.
The basic structure of programming language: The combination of various system commands;
Data storage: variables, arrays (advanced)
Expression: a+b
Statement: If
To create a shell script:
First step: Create an edit text file using a text editor (example VIM)
The first line must specify the shell declaration sequence (Magical head)
#! /bin/bash (shell type)
#添加注释 (such as function, date, etc.)
#开始的计算机不读是给用户看的.
Step two: Run the script
Script code Opening Conventions
1, the first line is generally called the language used
2, program name, avoid changing the file name is unable to find the correct file
3. Version number
4, the time after the change
5. Relevant information of the author
6, the role of the procedure, and matters needing attention
7, the final version of the update a brief description
Basic structure of the script
#! Define the variable definition function main code
Syntax error in detecting script: Bash-n
Debug execution: Bash-x/path/to/some_script
Weak type: The runtime of the language implicitly does the data type conversion. You do not need to specify a type, the default is character type, the participating operation will automatically do the implicit type conversion, the variable can be called directly without prior definition. The shell does not support floating-point (decimal)
Divide the following variable types according to criteria such as the effective range of the variables:
Local variable: The active scope is the current shell process and is not valid for Shell processes other than the current shell, including the current Shell's child shell process
Environment (GLOBAL) variable: The active scope is the current shell process and its child processes
Export defines environment variables export b=100 or declear-x b=100
Local variable: The effective range is a snippet of code in the current shell process, usually referred to as a function
Positional variables: $, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.
Special variables
$ : Indicates whether the execution of the previous command succeeded or failed: 0 for success, not 0 for failure
$: The command itself in positional variables
$
: All parameters in positional variables
[Email protected]: All parameters in positional variables
$#: Number of positional variables
$$: Displays the process number of the current shell
Show all defined variables: set; Delete variable: unset name
Variable reference: ${name} $name
"": weak reference, where the variable reference is replaced with the value of the variable
': Strong reference, where the variable reference is not replaced with the value of the variable, while preserving the original string
Read-only variables: can only be declared, but cannot be modified and deleted
– Declaring read-only variables: readonly name Declare-r name
– View read-only variable: readonly–p
Positional variables: Calling parameters passed to the script through the command line in script code
$, $, ... : corresponding to 1th and 2nd parameters, Shift [n] Change position
$: Command itself
$: All parameters passed to the script, all parameters are combined as a string
[Email protected]: All parameters passed to the script, each parameter is a separate string
$#: The number of arguments passed to the script
[Email protected] $* only when it's wrapped in double quotes.
Process uses exit status to report success or failure
? 0 stands for Success, 1-255 for failure
? $? Variable saves the most recent command exit status
Exit N: Custom exit status Code
Note: Once the exit command is encountered in the script, the script terminates immediately; the terminating exit state depends on the number following the Exit command
Note: If you do not specify an exit status code for the script, the exit status code for the entire script depends on the status code of the last command executed in the script
Logical operations
? Non -:!
! 1 = 0
! 0 = 1
? Short circuit operation
Short Circuit and
The first one is 0, and the result must be 0.
The first one is 1, the second must be involved in the operation
Short Circuit or
The first one is 1, and the result must be 1.
The first one is 0, the second must be involved in the operation
XOR: ^
XOR two values, same as false, different for true
Depending on the exit status, the command can be run conditionally
&& represents conditional and then
|| Represents a conditional or ELSE
Numerical comparison
Test "$A"-eq "$B" = ["$A"-eq "$B"] is judged true (the return value is 0) executes && the following instruction, the return value is not 0 execution | | The following instructions
-GT is greater than
-ge is greater than or equal to
-eq is equal to
-ne is not equal to
-lt is less than
-le is less than or equal to
The test usage is similar to [];
-A or-e filename to determine if the file exists, true if it exists
-B Determine if the file is a block device
-C Determine if the file is a symbol device
-D file: Exists and is a catalog file
-F file: exists and is a normal file
-H file or-L file: Existing and Symbolic link files
-P file: exists and is a named pipe file
-S file: exists and is a socket file
Test-v Bianliang; echo $? Determine if Bianliang is set
File permission test:
-R FILE: exists and is readable
-W FILE: exists and is writable
-X FILE: exists and is executable
File Special Permissions Test:
-U FILE: Exists and has suid permissions
-G FILE: Exists and has Sgid permissions
-K FILE: Exists and has sticky permissions
File size test:
-S FILE: exists and is not empty
Whether the file is open:
-T FD:FD file descriptor is already open on a terminal
-N File: Whether the file has been modified since the last time it was read
-O File: Whether the current active user is a file owner
-G file: whether the current active user is a group of files
Binocular test:
File1-ef file2:file1 is a hard link to FILE2
File1-nt File2:file1 is new to FILE2 (Mtime)
File1-ot File2:file1 is older than FILE2
string comparison
= = is the same [$test 1 = = $test 2];echo $?
! = or <> Indicates whether it is different
~= whether the left string can be matched by the pattern on the right [[AB =~ A]
[[B > A]] indicates the order in the default encoding table, followed by a larger row.
< [[a < b]]
-Z "string" to determine if the string is empty and true for null
-N "string" to determine whether the string is non-null and true for non-null
Use read to assign input values to one or more shell variables
-p Specifies the prompt to display
-s silent input, commonly used for passwords
n n Specifies the character length of the input
-d ' character ' input terminator
-T n timeout is n seconds
Read reads values from standard input, assigns a variable to each word
All remaining words are assigned to the last variable
Login shell and non-logon shell
Log-in Shell
Execution order:/etc/profile---/etc/profile.d/-------~/.bash_profile, ~/.BASHRC--and/ETC/BASHRC
Non-logon
Execution order:
~/.BASHRC--/ETC/BASHRC--/etc/profile.d/SH
Source and. Executes commands from a file under the current shell.
Using script execution is executing commands under a child shell, so the parent shell cannot inherit variables from the script
Command:env, set, unset, export, Declear, ReadOnly, let, help, test, [], read
Shell Script Basics