As Linux systems are used more and more in enterprises, automated management of servers has become more and more important
In the automatic maintenance of Linux servers, in addition to the setting of scheduled tasks, the application of shell scripts is also a very important part
What is the difference between shell and shell script? To be precise, Shell is a command line interpreter, its role is to follow a certain grammar to interpret the input commands and pass them to the system
The shell script is a program file that puts various commands into a file in advance, which is convenient for one-time execution. It is extremely efficient.
1. Shell script programming specification
1. Concept
In some complex Linux maintenance tasks, a large number of repetitive input and interactive operations are not only time-consuming and labor-intensive, but also error-prone
And write an appropriate shell script program, which can be processed in batches and complete a series of maintenance tasks automatically , which greatly reduces the burden on the administrator
Save the commands to be executed to a text file in order
Give the file executable permissions
Can be combined with various Shell control statements to complete more complex operations
2. Application scenarios
To summarize the above: Shell script is to save the commands to be executed to a text file in order, and give the file executable permissions to facilitate one-time execution of a program file , which is convenient for administrators to set or manage, and can be combined with various Shell control statements to complete more complex operations
Common scenarios are as follows:
Repetitive operation
Interactive task
Bulk transaction processing
Service running status monitoring
Scheduled task execution
…
3. Role
Command interpreter, "translator"
Between the system kernel and the user, responsible for interpreting the command line
Receive and explain the operation instructions input by the user, and then pass the explanation that needs to be executed to the kernel for execution and output the result
The shell program is used by default after login, generally /bin/bash
The internal commands and operating environment of different Shells will be different
Give executable permissions to the finished shell script file to make the script executable. The
above editing content includes three commands to realize the view of the files starting with vml in the /boot directory, including..., to realize the "semi-automatic process" ".sh "The role of "is a kind of identification, which is convenient for you and others to identify this is a shell script file
Empowerment:
Remember, it is generally necessary to empower, add executable permissions to ensure that the script can be executed normally
5. Three ways to execute script files
Take the above script as an example
Method 1 : Specify the path command, the file must have x permission. The script file path ( absolute path and relative path , and must be authorized)
Method 2 : Specify Shell to interpret the script, and the file does not require x permission
sh 脚本路径:sh first.sh
source 脚本路径:
. first.sh
或
source first.sh
6. How to further improve your script
Add more user-friendly, friendly prompt information in it, it is easy for you to review, and others can read
"#!/bin/bash is the default interpreter, there are other types of interpreters, such as python, etc." #: This symbol represents comment information and will not be executed. It is generally used to explain the role of this script, etc.
As you can see, there is a line of Chinese prompt followed by the output result, is it very comfortable to look at?
The execution method of ". Script file path" is to execute and perform the execution process without authorization, and the first two need to be authorized, and then only display the result without "executing"
7. Redirection and pipeline operation
Shell scripts are characterized by batch automated processing, and the operation process is located in the background, without any intervention by the user during the operation process
So how can you quickly find the information you want? This is often critical when troubleshooting
Therefore, it is very important to learn to extract and filter execution information!
7.1 Interactive hardware device
The Linux system uses files to describe various hardware and equipment and other resources. In the process of processing information from the operating system, users include the following three types of interactive hardware equipment
Standard Input : Receive user input data from the device
Standard output : output data to the user through the device
Standard error : report execution error information through the device
All of the above are the default use of keyboard and monitor as associated devices, interact with the operating system, complete the most basic input and output operations
Receive various command strings, auxiliary control and other information input by the user from the keyboard and output the results to the screen; if the command is wrong, it will also feedback error information to the screen
Types of
Device file
File description number
Default device
Standard input
/dev/stdin
0
keyboard
Standard output
/dev/stdout
1
monitor
Standard error output
/dev/stderr
2
monitor
You can change the direction of input and output content without using the default standard input and output devices (keyboard and display). This operation is called "redirection"
7.2 Redirection operation
Types of
Operator
use
Redirect input
<
Read data from the specified file instead of inputting it from the keyboard
Redirect output
>
Save the output result to the specified file (overwrite the original content)
/
>>
Append the output result to the end of the specified file
Standard error output
2>
Save the error information to the specified file (overwrite the original content)
/
2>>
Append the error message to the specified file
Mixed output
&>
Save the contents of standard output and standard error to the same file
Second, in-depth analysis of shell script variables
1. Role and type
Shell variables are used to store specific parameters (values) that the system and users need to use
Variable name: use a fixed name, preset by the system or user-defined
Variable value: can be changed according to the user's setting or the change of the system environment
Types of
Explanation
Custom variable
Defined, modified and used by the user
Environment variable
Maintained by the system for setting up the working environment
Location variable
Pass parameters to the script program through the command line
Predefined variables
A type of variable built in Bash, cannot be modified directly
1. Custom variables
Custom variables are variables defined by system users themselves, which are only valid in the user's own shell environment, so they are also called local variables
When writing a shell script, some specific custom variables are usually set
1.1 Define new variables
Variable names start with a letter or underscore, and are case sensitive. All uppercase letters are recommended for easy identification
The basic format for defining variables is " variable name=variable value "
Note that there are no spaces on either side of the equal sign!
Edit the script in the document to collect personal information
1.5 Set the scope of the variable
By default, newly defined variables are only valid in the current Shell environment, so they are called local variables. When entering a subroutine or a new sub-Shell environment, local variables can no longer be used
The specified variables can be exported as global variables through the internal command export, so that user-defined variables can continue to be used in all sub-Shell environments
格式1:export 变量名
格式2:export 变量名=变量值
Use the pstree command to view the Shell environment
Enter the bash command to enter the sub-Shell environment
Press the Ctrl+D key combination or enter the exit command to exit the sub-Shell environment, and test whether the set global variables take effect in the sub-environment
Environment variables are created in advance by the system to set the user’s working environment
It can be used to change or set an environment variable for a long time. The global configuration file is /etc/profile, and the variables defined in this file apply to all users.
The user's own independent profile (~/.bash_profile), the variables defined by this file only apply to the current user
2.2 Commonly used variables and how to view
Use the env command to view the environment variables in the current working environment
Commonly used variables
USER 表示用户名称
HOME 表示用户的宿主目录
LANG 表示语言和字符集
PWD 表示当前所在的工作目录
PATH 表示可执行程序的默认搜索路径
When executing command line operations, the first field represents the name of the command or script program, and the remaining string parameters are assigned to the position variables in order from left to right