Shell script (Shell Script) is a program file that saves the commands to be executed in a text file in order, and gives the file executable permissions to facilitate one-time execution. It is mainly convenient for administrators to set up or manage, and can be combined with Shell control statements to complete complex operations.
Generally used for repetitive operations, batch transaction processing, automated operation and maintenance, service status operation monitoring, timing task execution, etc.
1.2 Shell script programming specification
Shell script is a special application program, which is between the operating system kernel and the user. It acts as a "command interpreter". It is responsible for receiving and interpreting the operation instructions input by the user, and passing the operation to the kernel for execution and output Results of the.
1.2.1 Shell interpreter types
There are many types of interpreters, different shell scripts, internal commands, and command prompts will have some differences
[root@localhost opt]# vim first.sh ## 脚本文件以.sh 结尾
#!/bin/bash ## Shell解释器的程序为/bin/bash
#注释
cd /boot
pwd
ls -lh vml*
1.2.3 Shell script execution
Three ways:
./PATH## Absolutely can be relative to have executable X permissions
sh PATH## Execution permission is not required. It is generally recommended to keep the path unchanged after execution, and the environment has not changed.
source first.sh ## The path has changed, and its environment has changed,. xxx ## is equivalent to source
awk: regular expression, one of the three swordsmen to deal with the column -F: composition separator with a colon (F followed by a delimited symbol) write nothing The default is separated by tabs and spaces (-F does not write) print output $ position# # Separate with a comma
Two, variables
Shell variables are used to store specific parameters (values) required by the system and users. These parameters can be changed according to user settings or system changes. The values of variables are stored in memory.
Function: 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 (PATH) variable value: Can be changed according to user settings and changes in system environment
Variable type: custom variables: defined, modified and used by the user. Special variables, read-only variables, positional variables, and predefined variables ($?)
2.1 Define new variables
Variable names start with a letter or underscore, and are case sensitive, all uppercase is recommended
Basic format variable name = variable value (variable name on the left, variable value on the right, "=" is the assignment symbol)
[root@localhost ~]# name=zhangsan ## 把张三赋值给name
2.2 View and reference variable values
Add the symbol "$" before the variable name, you can refer to the value of a variable
You can use the echo command to view variables, and you can view multiple variable values in a single echo command.
Specifying the assignment content after the "=" is the most basic way to assign a value to a variable, and there are some operations that can assign a value to a variable more flexibly.
2.3.1 Double quotes (")
It is mainly used to define the character string, especially when the assignment content contains spaces, it must be enclosed in double quotes.
Allow reference to other variables through the $ symbol
[root@localhost ~]# abc=a b c ## 包含空格 无法赋值
-bash: b: 未找到命令
[root@localhost ~]# abc="a b c" ## 赋值成功
[root@localhost ~]# echo $abc
a b c
2.3.2 Single quote (')
When the content to be assigned contains characters with special meaning such as "$" "" "" \", it should be enclosed in single quotation marks.
Within the range of single quotation marks, the values of other variables cannot be quoted. Any character is treated as a normal character, but the assignment contains single quotation marks, which need to be escaped with ",".
When it is difficult to implement nested command operations in a line of commands using back apostrophes, you can use "$()" instead of back apostrophe operations.
It is a built-in command of bash. The read command is used to prompt the user to enter information, so as to implement a simple interaction process.
When executing, it will read a line of content from the standard input device (keyboard), and use the space as a separator to assign each read field to the specified variable one by one (the extra content is assigned to the last one), and there is only one specified variable. The entire line of content is assigned to this variable.
The read command can be combined with "-p" and "-t" to set prompt information and input waiting time
[root@localhost opt]# vim first.sh
read -p "请输入一个数值:" sum
echo "你输入的值是:$sum"
[root@localhost opt]# sh first.sh
请输入一个数值:1
你输入的值是:1
Three, 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 subprogram or a new child Shell script, the variables defined in the parent Shell environment cannot be referenced.
Variables defined by global variables can continue to be used in all sub-Shell environments, and the specified variables can be exported as global variables through the internal command Export
It is also possible to assign values to variables when global variables are everywhere.
[root@localhost ~]# export num3=3
Fourth, the calculation of numerical variables
Numerical operations of shell variables are mostly used for script process control (such as cycle times, usage comparison, etc.).
In the Bash Shell environment, only simple integer operations can be performed, and decimal operations are not supported. The calculation of integer value is mainly carried out through the internal command expr, the basic format is as follows
In addition to expr variable values, common commands include (()), let, etc. If you want to perform a simple operation, just enclose the specific arithmetic expression with "$((" and "))".
Environment variables refer to a type of variables created in advance by the Linux system for operational needs, and are mainly used to set the user's working environment. Including user home directory, command search path, user current directory, login terminal, etc.
The values of environment variables are automatically maintained by the Linux system and will change as the user's status changes.
Use the env command to view the environment variables in the current working environment. Common environment variables are: USER: indicates the user name HOME: indicates the user's home directory. LANG: indicates the language and character set PWD: indicates the current working directory PATH: indicates the command search path.
PATH is used to run and set the default search path of executable programs. When specifying the file name to execute the command program, the linux program will search for the corresponding executable file in the directory range specified by the PATH variable.
In the Linux system, the global configuration file of environment variables is /etc/profile, and the variables defined in this file are used for all users.
Each user has its own independent profile (~/.bash_profile).
5.2 Location variables
When using a linux script program, it is convenient to provide operating parameters for the program through the command line. Bash introduces the concept of location variables.
When executing command line operations, the first field represents the name of the command or script program, and the remaining strings are assigned to the positional variables in order from left to right.
Positional variables are also called positional parameters and are represented by $1, $2, $3, $4, $5, $6, and $7.
Predefined variables are a type of variables that are predefined by the Bash program. Users can only use predefined variables, but cannot create new predefined variables, nor can they directly assign values to predefined variables.