Shell script basic programming commands
- Shell programming specification
- Shell script composition
- The role and types of shell variables
Shell 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" and is responsible for receiving the operation instructions (commands) input by the user and interpreting the operations that need to be performed. Pass it to the kernel for execution and output the execution result.
Shell script concept
- 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
Shell script application scenarios
-
Repetitive operation
Sometimes you need to repeat the operation, you can write a script for loop implementation -
Interactive task
-
Bulk transaction processing
-
Service running status monitoring
-
Timing task execution
Write the script into crotable -e to go in for timing tasks
The role of Shell
-Command interpreter, "translator"
- Translator between the kernel and the user
Write script code
- Use vim text editor
- One Linux command per line, written
in the order of execution. When writing a shell script, put .sh after the name to indicate that this is a shell script command
Grant executable permissions
- Make the script executable attribute
chomd +x file
Execute script file
- Method 1: The script file path (absolute path and relative path)
must have x permission - Method 2: sh script file path
- Method 3: Source script file path
can also be executed by "."
Shell script composition
- Script statement is
also called "interpreter": if the first line is "#!/bin/bash", it means that the code statements below this line are interpreted and executed by the /bin/bash program, #! /bin/bash is the default interpreter, there are other types of interpreters - Comment information
- Executable statement
Redirection and pipeline operations
Interactive hardware device
- 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 this device
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 |
Redirect operation
Types of | Operator | use |
---|---|---|
Redirect input | < | Read data from the specified file |
Redirect output | > | Save the standard output result to the specified file and overwrite the original content |
>> | Append the standard output result to the end of the specified file without overwriting the original content | |
Standard error output | 2> | Save the error information to the specified file and overwrite the original content |
2>> | Append the error message to the end of the specified file without overwriting the original content | |
Mixed output | &> | Save standard output and standard error to the same file |
2>&1 | Redirect standard error output to standard output |
Pipe operation symbol "|"
- Output the result of the command on the left as the processing target of the command on the right
The role and types of shell variables
The role of variables
- 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 change according to user settings and changes in the system environment
Variable type
Custom variables: defined, modified and used by the user.
Special variables: environment variables, read-only variables, location variables, predefined variables
Define a new variable
- The variable name starts with a letter or an underscore and is case sensitive. All uppercase letters are recommended.
Variable name = variable value ("=" means assignment) - View the value of the
variable echo $variable name
- If you want to connect other content closely behind the output, you can't directly add it. At this time, we need to use "{}". Using this symbol, the above statement can be achieved. Or you can use "" to quote all the $ codes.
Use quotes when assigning values
-
Double quotes: Allow the use of the $ symbol to quote other variable values
-
Single quote: It is forbidden to quote other variable values, and $ is regarded as a normal character
-
Backtick: command substitution, extract the output result after command execution
Assign values to variables from keyboard input
* read [-p "提示信息"] 变量名
The scope of the variable
- By default, the newly defined variables are only valid in the current Shell environment, so they are called local variables, but when we enter the subroutine or the new sub-Shell environment, the local variables will no longer be usable. At this time, if we let us again It is undoubtedly a waste of time to configure variables. So we can export the specified variables into global variables through the internal command export , so that user-defined variables can be used again in all sub-Shell environments.
格式1:export 变量名
格式2:export 变量名=变量值
- We first check the content of the AI variable in our environment, and we can see it when we find it.
-
Then use pstree to enter the sub-bash environment of bash, and then use the variable AI we set in the bash environment, and then find that there is no such variable.
-
After that, we return to the bash environment and use the export command to set our AI variables as global variables.
- Back to the sub-bash environment again, at this time we found that this variable can also be used in the sub-bash environment, this variable is a global variable.
- The above method fails after restarting. At this time, if we want to be able to use it all the time, we need to enter export variable name=variable value in /etc/profile.
Operation of integer variables
expr variable 1 operator variable 2 [operator variable 3]...
Common operators
- Addition operation: +
- Subtraction operation:-
- Multiplication operation:*
- Division operation: /
- Modulus (remainder) operation:%
Note: This method is not concise enough, just for understanding
Concise algorithm
How to use [] and (())
* echo $[1+2]
* echo $((1+2))
* let i=1+2
* echo $i
Environment variable
- Created in advance by the system to set up the user’s working environment
- Configuration file: /etc/profile,~/.bash_profile
Common environment variables
- Use the env command to view the environment variables in the current working environment
USER:表示用户名称
HOME:表示用户的宿主目录
LANG:表示语言和字符集
PWD:表示当前所在的工作目录
PATH:表示可执行程序的默认搜索路径
- The global configuration file for environment variables is /etc/profile, and the variables defined in this file apply to all users. Each user has his own profile (~/.bash_profile). Can be used to change or set an environment variable for a long time.
Read-only variable
- Used when the variable value is not allowed to be modified
readonly name #设置为只读变量
Location variable
- Expressed as $n, n is a number between 1-9, 10 and subsequent numbers need to add {}
We can write a simple shall script to understand this definition in detail
Then we execute this script, and the numbers in different positions are written out by the script
Predefined variables
* $*, $@:表示命令或脚本要处理的参数。不加双引号时表现一致。
* $#:把所有参数看成以空格分隔的一个字符串整体(单字符串)返回,代表"$1 $2 $3 $4".
* $@:把各个参数加上双引号分隔成n份的参数列表,每个参数作为一个字符串返回,代表"$1" "$2" "$3" "$4".
* $*:所有位置变量的内容
* $?:上一条命令执行后返回的状态,当返回状态值为0时表示执行正常,返回任何非0值均表示出现异常。也常被用于Shell脚本中return退出函数并返回的退出值
* $0:当前执行的进程/程序名
- Since $@ and $* are a bit difficult to understand, I will write a script here to see the difference between the two