-
什么是shell脚本?
- shell命令的集合, 可以使用控制语句: for , if …else, while(), 这些命令写到一个文件中, 给这个文件一个可执行权限
-
基本格式?
-
命名
- 一般文件后缀为.sh - 推荐使用
- xxx.sh
- 这个.sh后缀并不是必须要加的
- xxx
- 一般文件后缀为.sh - 推荐使用
-
书写格式
-
第一行, 解析脚本命令的时候使用的命令解析器
#! /bin/bash shell 命令 * n /bin/sh - shell /bin/bash -
-
shell脚本的中命令的执行顺序
- 逐行解析
-
-
添加权限
chmod u+x xxx.sh
-
-
执行脚本
- ./xxx.sh - 用的比较多
- sh xxx.sh
3.2 shell中的变量
-
如何定义变量
// c/c++ int number; int num = 100; // shell, 变量没有数据类型, 必须给定义的变量初始化 value1="hello" value2=111 value3='world' 注意事项: 在赋值的时候, 等号前后不能有空格
-
位置变量 - 执行shell脚本时候, 传递到内部的参数
-
$1
-
$2
-
$3
-
$…
#! /bin/bash echo "$0" $0 echo "参数1" $1 echo "参数2" $2 echo "参数3" $3 echo "参数4" $4 # 执行脚本 ./aa.sh aa bb cc dd ee ff gg # 结果 ./aa.sh ./aa.sh 参数1 aa 参数2 bb 参数3 cc 参数4 dd
-
-
特殊变量
- $#: 执行脚本时候传递的参数的个数
- $@: 执行脚本时候, 传递的所有参数
- $$: 进程PID
- $?: 程序执行完成之后的返回值
-
取普通变量的值
- 普通变量名前边加上$
- $var
- ${var}
- 普通变量名前边加上$
-
取命令执行之后的得到的值
- 语法1: var=$(shell命令)
- $()
- 语法2: var= `shell命令`
- 反单引号 ``
- 语法1: var=$(shell命令)
3.3 awk介绍
awk 可以将文件拆分成功若干行, 根据指定的分隔符, 再将每一行拆分成若干列, 默认按照空格或tab进行拆分
-
基本语法格式
§ awk 参数 '条件{处理动作}' 操作的文件或数据 □ awk '$1 > 1000{num++; print num} § awk 参数 '/正则表达式{处理动作}' 操作的文件或数据 ps aux | awk '$2 > 60000{print $2}'
-
指定分隔符的参数: -F
- -F分隔符
- -F:
- -F分隔符
-
如何使用变量取出某一列?
- $0 - 代表当前行
- $1 - 第一列
- $2 - 第二列
- $3 …
以冒号分隔 如果第三列大于500 就输出第三列值
[root@localhost 2SecKeyServer]# awk -F: ‘$3 > 500 {print $3}’ /etc/passwd
3.4 条件判断语法
if [ -d aa ]; then
处理动作
elif [ -f aa ]; then
处理动作
处理动作
else
处理动作
处理动作
fi
if [ -d aa ]
then
处理动作
elif [ -f aa ]
then
处理动作
处理动作
else
处理动作
处理动作
fi
3.5 关闭对应进程的shell脚本
# 得到当前用户的用户名
USER=$(whoami)
# 获取当前进程的PID
PID=`ps -u $USER | grep SecKeyServer.ou | awk '{print $1}'`
if [ -z $PID ]; then
# pid为空
echo "pid为空"
else
# 非空字符串
# 给对应的进程发信号
kill -10 $PID
fi
shell条件测试参考
-
文件状态测试
-b filename 当filename 存在并且是块文件时返回真(返回0) -c filename 当filename 存在并且是字符文件时返回真 -d pathname 当pathname 存在并且是一个目录时返回真 -e pathname 当由pathname 指定的文件或目录存在时返回真 -f filename 当filename 存在并且是正规(普通)文件时返回真 -g pathname 当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真 -h/-L filename 当filename 存在并且是符号链接文件时返回真 (或 filename) -k pathname 当由pathname 指定的文件或目录存在并且设置了"粘滞"位时返回真 -p filename 当filename 存在并且是命名管道时返回真 -r pathname 当由pathname 指定的文件或目录存在并且可读时返回真 -s filename 当filename 存在并且文件大小大于0 时返回真 -S filename 当filename 存在并且是socket 时返回真 -t fd 当fd 是与终端设备相关联的文件描述符时返回真 -u pathname 当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真 -w pathname 当由pathname 指定的文件或目录存在并且可写时返回真 -x pathname 当由pathname 指定的文件或目录存在并且可执行时返回真 -O pathname 当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O 大写) -G pathname 当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真 file1 -nt file2 file1 比file2 新时返回真 file1 -ot file2 file1 比file2 旧时返回真 f1 -ef f2 files f1 and f2 are hard links to the same file -
常见字符串测试
-z string 字符串string 为空串(长度为0)时返回真 -n string 字符串string 为非空串时返回真 str1 = str2 字符串str1 和字符串str2 相等时返回真 str1 == str2 同 = str1 != str2 字符串str1 和字符串str2 不相等时返回真 str1 < str2 按字典顺序排序,字符串str1 在字符串str2 之前 str1 > str2 按字典顺序排序,字符串str1 在字符串str2 之后 -
常见数值测试
nt1 -eq int2 如果int1 等于int2,则返回真 int1 -ne int2 如果int1 不等于int2,则返回真 int1 -lt int2 如果int1 小于int2,则返回真 int1 -le int2 如果int1 小于等于int2,则返回真 int1 -gt int2 如果int1 大于int2,则返回真 int1 -ge int2 如果int1 大于等于int2,则返回真 -
测试时使用的逻辑操作符
-a 逻辑与,操作符两边均为真,结果为真,否则为假。 -o 逻辑或,操作符两边一边为真,结果为真,否则为假。 ! 逻辑否,条件为假,结果为真。