2023-08-15 09:19:49 +08:00
|
|
|
|
## 一、shell
|
|
|
|
|
|
|
|
|
|
### 1.1 shell 解释器
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
/bin/bash [默认]
|
|
|
|
|
/bin/sh
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.2 shell 脚本调用
|
|
|
|
|
|
|
|
|
|
系统自动调用:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
/etc/profile 系统环境变量, 每次系统启动时调用
|
|
|
|
|
~/.bashrc 用户环境变量, 用户登录时调用
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
用户调用:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
1) chmod +x xxx.sh
|
|
|
|
|
./xxx.sh
|
|
|
|
|
2) bash xxx.sh
|
|
|
|
|
3) . xxx.sh 相当于 source xxx.sh
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.3 shell 脚本的定义
|
|
|
|
|
|
|
|
|
|
第一行:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.4 shell 变量
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
定义: 变量名=值
|
|
|
|
|
引用: $变量名, "$变量名 xxx"
|
|
|
|
|
重新赋值: 变量名=新值
|
|
|
|
|
算术计算: 变量名=$(( 变量名 算术运算符 值 ))
|
|
|
|
|
|
|
|
|
|
预定变量: $#, $*, $?, $1~9, $0 进程名, $$ 进程ID
|
|
|
|
|
环境变量: $环境变量名
|
|
|
|
|
export 变量名=值 设置环境变量
|
|
|
|
|
|
|
|
|
|
特殊的变量写法: (变量名=值; 其它语句) 不影响外部的同名变量
|
|
|
|
|
{变量名=值; 其它语句;} 影响外部的同名变量
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.5 条件测试语句
|
|
|
|
|
|
|
|
|
|
语法:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
test 操作符 变量
|
|
|
|
|
test 变量1 操作符 变量2
|
|
|
|
|
[ 操作符 变量 ]
|
|
|
|
|
[ 变量1 操作符 变量2 ]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
文件相关:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
-e 是否存在 -d 是目录 -f 是文件
|
|
|
|
|
-r 可读 -w 可写 -x 可执行
|
|
|
|
|
-L 符号连接 -c 是否字符设备 -b 是否块设备
|
|
|
|
|
-s 文件非空
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
字符串相关:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
= 两个字符串相等 != 两个字符串不相等
|
|
|
|
|
-z 空串 -n 非空串
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
数字相关:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
-eq 数值相等 -ne 数值不相等
|
|
|
|
|
-gt 数 1 大于数 2 -ge 数 1 大于等于数 2
|
|
|
|
|
-lt 数 1 小于数 2 -le 数 1 小于等于数 2
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
复合测试相关:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
command1 && command2 左边命令执行成功(即返回 0)shell 才执行右边的命令
|
|
|
|
|
command1 || command2 左边的命令未执行成功(即返回非 0)shell 才执行右边的命令
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
多重条件判定:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
-a 多个条件必须都为真(true) 结果才为true
|
|
|
|
|
-o 多个条件中只需要一个为真,结果为true
|
|
|
|
|
! 条件取反
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### 1.6 分支语句
|
|
|
|
|
|
|
|
|
|
if 分支:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
if [ 条件1 ]; then
|
|
|
|
|
执行第一段程序
|
|
|
|
|
elif [条件2 ]; then
|
|
|
|
|
执行第二段程序
|
|
|
|
|
else
|
|
|
|
|
执行第三段程序
|
|
|
|
|
fi
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
case 分支:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
case $变量名称 in
|
|
|
|
|
“第一个变量内容”)
|
|
|
|
|
程序段一
|
|
|
|
|
;;
|
|
|
|
|
“第二个变量内容”)
|
|
|
|
|
程序段二
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
其它程序段
|
|
|
|
|
exit 1
|
|
|
|
|
esac
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
for 语句:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
for (( 初始值; 限制值; 执行步阶 ))
|
|
|
|
|
do
|
|
|
|
|
程序段
|
|
|
|
|
done
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
while 语句:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
while [ condition ]
|
|
|
|
|
do
|
|
|
|
|
程序段
|
|
|
|
|
done
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
until 语句:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
until [ condition ]
|
|
|
|
|
do
|
|
|
|
|
程序段
|
|
|
|
|
done
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
循环语句中可以使用 break 和 continue。
|
|
|
|
|
|
|
|
|
|
### 1.7 函数
|
|
|
|
|
|
|
|
|
|
定义函数:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
函数名(){
|
|
|
|
|
命令 ...
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function 函数名(){
|
|
|
|
|
命令 ...
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
调用函数:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
函数名 param1 param2……
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
函数体内,可以使用$1,$2 ...$9 读取传递到函数的参数。
|