When managing server clusters, it is necessary to write shell programs for server management.
The shell is a command line interpreter that provides users with an interface system-level program that sends requests to the Linux kernel to run programs. Users use the shell to start, suspend, stop, and write some programs.
Linux-Beginner Series 7_shell programming
1. Execution method of shell script
1. Script format requirements
Scripts start with #!/bin/bash
#!/bin/bash
The script needs to have executable permission
2. Common execution methods of scripts
01-Enter the absolute path or relative path of the script
To give the +x permission to the script, execute the script.
02-sh+script
Execute directly without giving the script +x permission.
Two, shell variables
1. Variable introduction
The variables in the Linux shell are divided into: system variables and user-defined variables.
01-system variable
[root@bogon shcode]# echo $HOME
/root
[root@bogon shcode]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@bogon shcode]# echo $PWD
/root/shcode
Display all variables in the current shell
set
02-shell variable definition
1) Basic grammar
define variable
变量名=值
[root@bogon shcode]# vim var.sh
#!/bin/bash
#定义变量A
A=100
#输出变量需要加上$
echo A=$A
[root@bogon shcode]# chmod u+x var.sh
[root@bogon shcode]# ll
总用量 8
-rw-r--r--. 1 root root 30 5月 7 10:52 hello.sh
-rwxr--r--. 1 root root 70 5月 7 11:17 var.sh
[root@bogon shcode]# ./var.sh
A=100
undo variable
unset 变量
declare static variable
readonly变量
Note: Cannot unset.
tips:
If you want to find line 15, show the line number
:nu
2) Rules for defining variables
-
Variable names can be composed of letters, numbers, and underscores, but cannot start with a number
For example: 5A=200(x)
-
There cannot be spaces on both sides of the equal sign
-
Variable names are generally uppercase
Assign the command's return value to a variable
反引号:运行里面的命令,并把结果返回给变量A
A=`zhang`
A=$(zhang)
3. Set environment variables
1. Basic grammar
export 变量名=变量值
//将shell变量输出为环境变量/全局变量
source 配置文件
//修改后的配置信息立即生效
echo $变量名
//查询环境变量的值
Multi-line comments for shell scripts
:<<! 内容!
Fourth, the position parameter variable
When executing a shell script, if you want to obtain the parameter information of the command line, you can use the positional parameter variable.
./myshell.sh 100 200
This is a command line to execute the shell, and the parameter information can be obtained in the myshell script.
basic grammar
grammar | describe |
---|---|
$n | n is a number $0 stands for the command itself, 1 − 9 stands for the first to ninth parameters, and more than ten parameters need to be enclosed in braces: 1-9 stands for the first to ninth parameters, and more than ten parameters need to be wrapped in curly braces Include:1−9 represents the first to ninth parameters, and more than ten parameters need to be enclosed in braces: {10} |
$* | For all parameters in the command line, $* regards all parameters as a whole |
$@ | Represents all parameters in the command line, but $@ treats each parameter differently |
$# | An individual representing all arguments on the command line |
5. Predefined variables
The variables defined in advance by the shell designer can be used directly in shell scripts.
basic grammar
grammar | describe |
---|---|
$$ | The process number PID of the current process |
$! | The process number PID of the last process running in the background |
$? | The return status of the last executed command. If the value of this variable is 0, it proves that the previous command was executed correctly. If the value of this variable is non-zero (the specific number is determined by the command itself), it proves that the previous command was executed incorrectly. |
6. Operators
Perform various operations in the shell.
1. Basic grammar
01-First way
"$((运算式))"
02-Second way
"$[运算式]"
03-The third way
expr m + n
There must be a space between expr operators.
If you want the result of expr to be assigned to a variable, use backticks ``.
expr \*,/,% 乘,除,取余
practise
Seven, conditional judgment
1. Basic grammar
#condition前后要有空格
[ condition ]
#非空返回true,可以使用$?验证(0为true,>1为false)
2. Judgment statement
Common Judgment Conditions
01-string comparison
=
02- Comparison of two integers
sentence | describe |
---|---|
-lt | less than |
- the | less than or equal to |
-eq | equal |
-gt | more than the |
-ge | greater or equal to |
- is | not equal to |
03- Judgment according to file permissions
grammar | describe |
---|---|
-r | read permission |
-w | write permission |
-x | permission to execute |
practise
3. Process control
01 if judgment
basic grammar
if [ 条件判断式 ]
then
代码
fi
if [ 条件判断式 ]
then
代码
elif [ 条件判断式 ]
then
代码
fi
Note: [Conditional Judgment], there must be a space between the square brackets and the conditional judgment.
02 case statement
basic grammar
case $变量名 in
"值1")
如果变量的值等于值1,则会执行程序1
;;
"值2")
如果变量的值等于值2,则会执行程序2
;;
...省略其他分支...
*)
如果变量的值都不是以上的值,则会执行此程序
;;
esac
03 for loop
Basic Grammar 1
for 变量 in 值1 值2 值3...
do
程序
done
Basic Grammar 2
for ((初始值;循环控制条件;变量变化))
do
程序
done
04 while loop
basic grammar
while [ 条件判断式 ]
do
程序
done
Eight, read read console input
basic grammar
read(选项)(参数)
options
options | describe |
---|---|
-p | Specifies the prompt when reading values |
-t | Specify the time (in seconds) to wait when reading the value, if it is not entered within the specified time, it will not wait anymore |
parameter
变量:指定读取值的变量名
Example:
Nine, function
Shell programming, like other programming languages, has system functions and can also customize functions.
1. System functions
01 basename basic syntax
Returns the last / part of the full path, often used to get the file name.
basename [pathname] [suffix]
Used to get the filename (not including the path). where,
pathname
denotes the file path to process, andsuffix
denotes the suffix to remove from the filename.
For example:
The following command to get the filename of the file:
If we want to remove
.txt
the suffix, we can use the following command
02 basic grammar of dirname
Used to get the file path (not including the file name).
dirname [pathname]
pathname
Indicates the file path to process.
2. Custom functions
basic grammar
[function] funname[()]
{
Action;
[return int]
}
调用函数名:funname [值]
function
Used to declare a function, the function name can be named arbitrarily.