Shell scripting is one of the skills that Linux OPS engineers have to master, and the use of shell scripts allows us to better operate Linux systems and facilitate our execution.
One, the basics of programming
BASIC Programming Concepts
Programming Logic processing methods: Sequential execution, loop execution, select execution
Program: Instruction + data
Shell programming: procedural, interpreting execution
Shell program: Provides programming capabilities to interpret execution
Program Programming Style:
Program: command-centric, data-serving instruction
Object type: Data-centric, instruction serves data
How the program is executed
Computer: running binary instructions;
The basic structure of the programming language:
Data storage: variables, arrays
Expression: A + b
Statement: If
Programming Languages:
Low Level: Compilation
Senior:
Compiling: High-level language--compiler--Target code: java,c#
Explanation: Advanced language--interpreter--Machine code: Shell, Perl, Python
Programming language classification
Strongly typed: You must specify a type when defining a variable, a join operation must conform to a type requirement, and calling an undeclared variable produces an error
such as Java,python
Weak type: No need to specify type, default is character type, participate in operation will automatically do implicit type conversion, variable without prior definition can be called directly
For example: Bash does not support floating-point numbers
Second, Shell scripting basics
Shell scripting specifications and uses:
A shell script is a text file that contains some commands or declarations and conforms to a certain format
Format requirements: First line shebang mechanism
Shell script: #! /bin/bash; file name ends with. sh
Python: #! /usr/bin/python; file name ends with. py
Perl: #! /usr/bin/perl; file name ends with. pl
The uses of shell scripts are:
Automating common commands
Perform system administration and troubleshooting
Create a simple application
Working with text or files
Create a shell script
First step: Use a text editor to create a text file
The first line must include the shell declaration sequence: #!: #!/bin/bash
Add comments: Comments begin with #
Step two: Run the script
Give execute permission to specify the absolute or relative path of the script on the command line
Run the interpreter directly and run the script as a parameter of the interpreter program
Script debugging
Bash-n/path/to/some_script
Detecting syntax errors in a script
Bash-x/path/to/some_script
Debug execution
Third, the basis of the script variable
Variable (the contents of a variable can be changed, it can store many types of data)
Variable: named memory space
How data is stored:
Character:
Value: Integer, floating-point
Variable: Variable type
Function: 1, data storage format; 2, participating operation; 3. Range of data represented
Type:
Character
Value: Integer, floating-point
Variable naming laws:
1. The reserved words in the program cannot be made: for example if, for;
2, only use numbers, letters and underscores, and cannot start with a number
3. See the meaning of the name
4, the uniform naming rules: Hump naming Law (multiple words named, divided into big, small hump, Big Hump is the first letter of each word capitalized, small hump is the first word except the first letter of lowercase other words uppercase)
{
Sleep hibernation: Sleep + time in the script can have time to see the parent-child process, and the sleep default time is seconds.
You cannot have spaces when defining variables, and you must expand them with double quotes.
echo defaults to one line, and the double quotation marks are displayed.
}
Iv. Introduction to the scope of the variable
Based on criteria such as the scope of the variable's entry:
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
Variable assignment: name= ' value '
You can use reference value:
(1) can be a direct string; Name= "Root"
(2) Variable reference: name= "$USER"
(3) Order reference: Name= ' command ', name =$ (command)
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
Show all variables that have been defined: set
Delete variable: unset name
Environment variable: The active scope is the current shell process and its child process local variables: The effective range is a snippet of code in the current shell process (usually referred to as a function)
Variable declaration, assignment:
Export Name=value
Declare-x Name=value
Variable reference: $name, ${name}
Show all environment variables:
Export
Env
Printenv
Delete: unset name
Bash has many built-in environment variables: PATH, SHELL, Usre,uid,histsize, HOME, PWD, Oldpwd, Histfile, PS1
Read-only variables: sound only, but cannot be modified and deleted (generally used on constants)
ReadOnly Name
Declare-r Name
(declare-rx name defines an environment variable even if it is a constant)
Positional variables: $, $, ... To indicate that the script is used to invoke parameters passed to it through the command line in the script code.
Calling parameters passed to the script from 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 into 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.
Example: Determining the number of rows for a given file
Linecount= "$ (wc-l $1| cut-d"-f1) "
echo "has $linecount lines."
Special variables: $?, $, $*, [email protected], $#
basename + file name
The syntax for basename is: basename[options [parameters]
Options: For file names with path information, such as/home/test/test.txt
Parameter: Refers to the file name extension
Five, bash operation
Arithmetic operations: Help let (bash does not support floating point, but still supports subtraction)
+,-, *,/,% modulus (remainder), * * (exponentiation)
To implement arithmetic operations:
(1) Let var= arithmetic expression
(2) var=$[arithmetic expression]
(3) var=$ (arithmetic expression)
(4) var=$ (expr arg1 arg2 arg3 ...)
(5) declare–i var = value (need to declare the value first, in the calculation)
(6) echo ' Arithmetic expression ' | Bc
Multiplication symbols need to be escaped in some scenarios, such as *
Bash has built-in random number generator: $RANDOM (1-32767)
echo $[$RANDOM%50]: 0-49
(Echo $[random%50+1]: 1-50%50+1:0-49 integrity +1, not single 49+1)
$? is to view the status of the previous command,
Expr uses multiplication sign to escape multiplication sign and add spaces such as: Expr 2 \* 3
Assign value
Enhanced Assignment: + =,-=, *=,/=,%=
{
% : In arithmetic operations, this is the modulo operator, that is, the remainder of the two numbers after the division operation;
}
Let Varopervalue
For example: Let count+=3 (self-add 3 before you assign value)
Self-increment, self-reduction:
Let Var+=1
Let var++
Let Var-=1
Let var--
Logical operations
True 1, False 0,
And:
1 and 1 = 1
1 and 0 = 0
0 and 1 = 0
0 and 0 = 0
Or:
1 or 1 = 1
1 or 0 = 1
0 or 1 = 1
0 or 0 = 0
Non -:!
! 1 = 0
! 0 = 1
Short-circuit Operation:
Short circuit with: (&&)
The first one is 0, the result must be 0;
The first one is 1, the second must be involved in the operation;
CMD1 && CMD2
CMD1 successful, will execute CMD2
CMD1 failed and will not execute CMD2
Short circuit or: (| | )
The first one is 1, the result must be 1;
The first one is 0, the second must be involved in the operation;
CMD1 successful, will not execute CMD2
CMD1 failure, will execute CMD2
XOR: ^
XOR two values, same as false, different for true
{
The difference between short-circuit and ordinary
CMD1 && CMD2
Short circuit with: CMD1 is true, execution cmd2, CMD1 is false, do not perform cmd2 direct exit.
Ordinary and: Cmd1 whether it is true or FALSE, Cmd2 to execute.
Short-circuit or common or difference
cmd1 | | Cmd2
Short circuit and: CMD1 is true, you can exit without execution Cmd2, CMD1 is False, execute cmd2.
Ordinary and: Cmd1 whether it is true or FALSE, Cmd2 to execute.
}
Six, Bash's exit status
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 (the state of the last command)
For example:
$ ping-c1-w1 Hostdown &>/dev/null
$ echo $?
(Ping-c1 for a spell) (Ping-w1 a second, the default is five seconds, in the LAN generally up to a second to connect the display information can be returned, the Internet time a little longer)
Exit Status Code
Bash Custom exit status code
Exit [n]: Custom exit status code (in script (exit) executes child shell)
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
Seven, Bash's test type
Condition test
To determine whether a demand is satisfied, it needs to be realized by testing mechanism;
A dedicated test expression needs to be assisted by a test command to complete the test process;
Evaluate Boolean declarations for use in conditional execution
If true, then return 0
If False, 1 is returned
Test command:
Test EXPRESSION (Test $a = $b tests if $ A and $b are both $a and $b must have a space before and after, if not, it will be assigned.
[EXPRESSION]
[[EXPRESSION]] ([[$a]] test $ A to define the value)
Note: You must have a white space character before and after EXPRESSION
Test command
Examples of long formats:
$ test "$A" = = "$B" && echo "Strings is equal"
$ test "$A"-eq "$B" \
&& echo "Integers is equal"
Examples of shorthand formats:
$ ["$A" = = "$B"] && echo "Strings is equal"
$ ["$A"-eq "$B"] && echo "Integers is equal"
Conditional execution Operators
Depending on the exit status, the command can be run conditionally
&& represents conditional and then
|| Represents a conditional or ELSE
For example:
$ grep-q no_such_user/etc/passwd \| | Echo ' No such user '
No such user
$ ping-c1-w2 station1 &>/dev/null \> && echo "Station1 is up" \> | | (Echo ' station1 is unreachable '; exit 1)
Station1 is up
Numerical test:
-GT: Is it greater than;
-ge: is greater than or equal to;
-eq: Is it equal to;
-ne: is not equal to;
-LT: Is it less than;
-le: is less than or equal to;
String test:
= =: whether equal;
ASCII code is greater than ASCII code
<: is less than
! =: is not equal to
=~: Whether the left string can be matched by the pattern on the right (the right side is to use an extended regular expression)
Note: The operands used for string comparisons should all use quotation marks
{
-Z "string": whether the string is empty, empty is true, not empty is false
-N "string": whether the string is not empty, not empty is true, empty is false
Note: This expression is typically used in [[]];
}
File test
Presence Testing
-a FILE: Same-E
-E File: The existence of the test, the existence of the true, otherwise false;
Presence and category Testing
-B File: Whether it exists and is a block of device files;
-C File: Existence and character device files;
-D file: exist and are directory files;
-F file: exists and is a normal file;
-H file or-L files: exists and is a symbolic link;
-P file: exists and is a named pipe file;
-S file: exists and is a socket file;
File Permissions Test: (is the actual permission to the standard, not to see the permissions set.) )
-R FILE: exists and is readable
-W FILE: exists and is writable
-X FILE: exists and is executable
File Special Permissions Test:
-G FILE: Exists and has sgid permissions;
-U FILE: Exists and has suid 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 indicates whether the file descriptor is open and related to 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 whether File2:file1 and FILE2 point to the same inode on the same device
{
Whether File1-nt File2:file1 is new to FILE2;
File1-ot File2:file1 is older than FILE2;
} (compared to the time the file was modified)
Combination test conditions
The first way:
COMMAND1 && COMMAND2 and
COMMAND1 | | COMMAND2 or
! COMMAND Non-
such as: [-e file] && [-R File]
The second way:
Expression1-a EXPRESSION2 and
Expression1-o EXPRESSION2 or (or)
! EXPRESSION
Must be performed using a test command;
# [-Z ' $HOSTNAME "-o $HOSTNAME" ==\ "Localhost.localdomain"] && HOSTNAME www.magedu.com
# [-f/bin/cat-a-x/bin/cat] && cat/etc/fstab
Eight, gather command
There are two ways to gather commands:
Compound: date; W.H.O. | Wc-l (command will run one after the other)
Child Shell: (date; who | wc-l) >>/tmp/trace (all outputs are sent to a single stdout and stderr)
Introduction to the basic shell