In some complex Linux maintenance work, a lot of repetitive inputs and interactions are time-consuming and error-prone. Scripts are needed.
The benefits of scripting: batch processing, automated completion of maintenance, reduction of the burden of a light administrator.
The Linux shell script is a special kind of application, the common shell interpreter has many kinds, uses the different shell time internal instruction: Cat/etc/shells
[Email protected] ~]# Cat/etc/shells
/bin/bash is the default shell interpreter in most Linux. All subsequent scripts are written in a bash script.
1. Writing the first shell script
Put the order of the usual operations command into the file to give execute permission, one time execution.
Let's write the first script first.sh
Linux does not prefix the file, for the convenience of memory here I will end with. Sh
[Email protected] ~]# vim first.sh
Comments:
#! /bin/bash is mostly about declaring that all I write is the bash language (I'm using bash interpreter)
Second behavior Comment Line, comment information does not take effect
When writing a larger script, if there is no good comment, then no one will be able to understand the meaning of it
Execute the command again.
Execution process:
[email protected] ~]# ll first.sh #查看是否具有执行权限
-rw-r--r--. 1 root root 22:58 first.sh
[email protected] ~]# chmod +x first.sh #给脚本添加执行权限
[email protected] ~]# ll first.sh #查看脚本是否具有执行权限
-rwxr-xr-x. 1 root root 22:58 first.sh
[email protected] ~]#/first.sh #执行脚本
Results of execution:
e - different ways to execute scripts
The first uses absolute path execution
The second method of using relative paths, such as./
The third use of the SH command to execute the format SH script name does not need to execute permissions-x parameter
The fourth kind uses. (space) The way the script name executes does not require EXECUTE permission. first.sh
The fifth use of the source script name does not require EXECUTE permission (mainly for the effective configuration file)
#建议使用后三种, in the production environment do not easily give the file executable permissions;
2. Variables in the script.
A variable is defined as a space that can hold a variable value
Changes can be made in different environments, which is a variable value.
By default: Each shell can be viewed as a different execution environment in Linux, so the same variable name has different values in different variable execution environments.
Common shell variable classifications:
Custom variables, environment variables, positional variables, pre-defined variables
The output of the variable
General use ECHO output variable echo $ variable name
Example 1: To define a variable named Linux with a value of 7.2
[Email protected] ~]# linux=7.2 #为变量Linux赋值
[Email protected] ~]# echo $Linux #输出变量Linux的值
7.2
[Email protected] ~]# linux=6.5 #为变量linux赋值
[Email protected] ~]# echo $linux #输出变量linux的值
6.5
You can define a variable directly on the command line and assign a value to the output variable through echo $ is a special character that refers to a variable (must use the $ symbol)
Note:Echo there must be a space between the variable and the call.
The value of the case variable is different.
Example 2: When you need to call two sets of variables together
[Email protected] ~]# echo $Linux $linux
7.2 6.5
Use echo directly after the variable called with $ if there are multiple spaces separated by
Example 3: You should use {} to enclose the variable name when the variable name and subsequent characters are easily confused
[[email protected] ~]# echo System${linux}
system7.2
[Email protected] ~]# echo ${linux}system
7.2system
Other Special Operations
Double quotation marks ("")
When a space is assigned to the right of the = sign, you need to use double quotation marks to expand it
[[email protected] ~]# webserver= "Nginx 1.1"
[Email protected] ~]# echo $webserver
Nginx 1.1
#在双引号的范围内还可以引用其他的变量 so that the existing variables can be assigned to the new variable
[Email protected] ~]# linux=7.2
[Email protected] ~]# system= "Rhel$linux"
[Email protected] ~]# echo $system
RHEL7.2
[Email protected] ~]#
Single quotation mark (')
when the content to be assigned includes "$", "\" and so on, special characters with other meanings should be enclosed in single quotation marks;
Other values cannot be referenced within the single quotation mark range, any word nonspacing as a normal character, but the contents of the assignment contain single quotation marks that need to be escaped with the \ ' symbol to avoid collisions.
[Email protected] ~]# Kernel=3.10$linux
[Email protected] ~]# echo $kernel
3.107.2 #这个结果不是我们想要的, the result we want is $3.10$linux.
[Email protected] ~]# kernel= ' 3.10$linux '
[Email protected] ~]# echo $kernel
3.10$linux
[Email protected] ~]#
Anti-apostrophe (")
position The key below the keyboard ESC.
The inverse apostrophe is primarily used for command substitution, allowing the output of a command's screen to be assigned to a variable.
Example: Find detailed information about a program on the command line
[Email protected] ~]# rpm-qf ' which pwd '
Coreutils-8.22-15.el7.x86_64
[[email protected] ~]# RPM-QF $ (which pwd)
Coreutils-8.22-15.el7.x86_64
[email protected] ~]# which PWD
/usr/bin/pwd
[Email protected] ~]# rpm-qf/usr/bin/pwd
Coreutils-8.22-15.el7.x86_64
[Email protected] ~]#
#反撇号括起来的范围内必须是可执行的命令. Otherwise there will be an error
It is important to note that it is difficult to use a backslash to implement a nested command in a command, which can be $ () in place of the anti-apostrophe
If the use of anti-apostrophe nesting will be wrong!!
[Email protected] ~]# rpm-q ' rpm-qf ' which pwd '
Rpm:no arguments given for query
Which-2.20-7.el7.x86_64
Package pwd are not installed
It is recommended to nest in the form of $ ()
[[email protected] ~]# Rpm-q $ (RPM-QF $ (which pwd))
Coreutils-8.22-15.el7.x86_64
Read Command
In addition to the above assignment you can use the Read command to assign values, the Read command is used to prompt the user to enter information, so as to achieve a simple interactive process (in fact, we entered the command is an interactive process)
You need to read a line from the standard input device keyboard and use a space as a delimiter when executing
[[email protected] ~]# Read kernel Linux # define two variable operations
4.7.2 7.2 ---Manually entered variable values
[Email protected] ~]# echo $kernel
4.7.2
[Email protected] ~]# echo $Linux
7.2
For more interactive images, improve ease of use, plus the-p option to set the cue message
[[email protected] ~]# read-p "Input your password:" passwd #-p specify prompt information
Input your password:123456 #123456就是 value of $passwd
[Email protected] ~]# echo $passwd #输出变量
123456
---------------------------------------------------------------------------------------------------------------
the above actions are only valid in the current bash environment, and will not be valid for other consoles or other Shel .
We enter the child shell validation of the current shell:
[Email protected] ~]# echo $Linux #在当前的shell环境输出变量
7.2
[Email protected] ~]# bash #切换子shell
[Email protected] ~]# echo $Linux #在子shell环境输出变量
#没有输出东西.
[Email protected] ~]#
Shell Script One