20.1shell脚本介绍
shell脚本介绍
- shell是一种脚本语言 和传统的开发语言比较,会比较简单
- shell有自己的语法;可以使用逻辑判断、循环等语法
- 可以自定义函数,目的就是为了减少重复的代码
- shell是系统命令的集合
- shell脚本可以实现自动化运维,能大大增加我们的运维效率
20.2 shell脚本结构和执行
shell脚本结构和执行
- 开头需要加#!/bin/bash
- 以#开头的行作为解释说明
- 脚本的名字以.sh结尾,用于区分这是一个shell脚本
- 执行方法有两种
- chmod +x 1.sh; ./1.sh
- bash 1.sh
- 查看脚本执行过程 bash -x 1.sh
- 查看脚本是否语法错误 bash -n 1.sh
shell脚本结构和执行
- 创建一个shell文件夹,并切入到文件夹中
[root@hanfeng ~]# mkdir shell
[root@hanfeng ~]# cd shell/
[root@hanfeng shell]#
- 写shell脚本
- #! /bin/bash //第一行必须这么写,固定格式,作用就是脚本若是在当台机器上去执行,可以不加这一行也没关系,因为它知道下面若干条的命令能在这台机器上去执行,去解析
[root@hanfeng shell]# vi 01.sh
#! /bin/bash //固定格式
echo "123"
w
ls
保存退出
- 执行脚本——>sh 01.sh
[root@hanfeng shell]# sh 01.sh
123
22:45:12 up 14 min, 1 user, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 22:32 0.00s 0.05s 0.03s w
01.sh
[root@hanfeng shell]#
- 在当前终端里,把01.sh中的#! /bin/bash 去掉后在执行脚本,会看到得到的结果相同,不会出现任何的问题,这就说明这台机器是能识别里面一条一条的命令的,去运行这里面的命令;但若是换一台机器,就不一定能执行了
- 在第一行,文件头指定 #!/bin/bash ,接下来要运行的命令是通过哪一个解释器来操作的,通常都是 /bin/bash 解释器来执行的
- 给01.sh文件一个执行权限
[root@hanfeng shell]# chmod a+x 01.sh
[root@hanfeng shell]#
7执行shell脚本 ./01.sh 能这样执行,说明这些命令被解析了,被/bin/bash认识了
[root@hanfeng shell]# ./01.sh
123
23:11:21 up 41 min, 1 user, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 22:32 1.00s 0.05s 0.00s /bin/bash ./01.sh
01.sh
[root@hanfeng shell]#
- /bin/bash也是一条命令, /bin/bash 和 /bin/sh 是同一个文件
[root@hanfeng shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960368 6月 10 2014 /bin/bash
[root@hanfeng shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 10月 21 00:47 /bin/sh -> bash
[root@hanfeng shell]#
- 01.sh文件内容就是被/bin/bash所解析的
- 若没有 /bin/bash ,可以使用 /bin/bash 01.sh去执行
[root@hanfeng shell]# /bin/bash 01.sh
123
23:21:43 up 51 min, 1 user, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 22:32 7.00s 0.03s 0.00s w
01.sh
[root@hanfeng shell]#
- 若是在shell脚本中在写入第二遍 #号开头的行, 就表示解释说明的作用
- 脚本的一般都是以.sh结尾,是为了区分这是一个shell脚本,否则需要打开这个文件,才知道是shell脚本
- 运行shell脚本有两种方法
- 一种是sh 01.sh运行shell脚本
- 另一种方法
- 先 chmod a+x 1.sh 给文件加一个执行权限
- 再 ./1.sh 去执行
- 这里的 ./ 就相当于一个相对路径,相对当前一个路径
- 也可以使用绝对路径去执行脚本 /root/shell/01.sh ,其实就是找到这个文件去执行
[root@hanfeng shell]# /root/shell/01.sh
123
23:45:02 up 1:14, 1 user, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 22:32 6.00s 0.03s 0.00s w
01.sh
[root@hanfeng shell]#
- 查看脚本执行过程 sh -x 01.sh 或者bash -x 01.sh
- -x,就是显示脚本执行的过程
- 每一个加号,表示一个操作步骤
[root@hanfeng shell]# sh -x 01.sh
+ echo 123
123
+ w
23:47:35 up 1:17, 1 user, load average: 0.00, 0.01, 0.05
USER TTY LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 22:32 7.00s 0.03s 0.00s sh -x 01.sh
+ ls
01.sh
[root@hanfeng shell]#
- 查看脚本是否有错误 sh -n 01.sh
- 若是没有任何的输出,那么脚本则没有错误
- sh -n 01.sh命令是检测是否存在语法错误
[root@hanfeng shell]# sh -n 01.sh
[root@hanfeng shell]#
20.3 date命令用法
date命令用法
- date +%Y-%m-%d, date +%y-%m-%d 年月日
- date +%H:%M:%S = date +%T 时间
- date +%s 时间戳
- date -d @1504620492
- date -d "+1day" 一天后
- date -d "-1 day" 一天前
- date -d "-1 month" 一月前
- date -d "-1 min" 一分钟前
- date +%w, date +%W 星期
date命令用法
- date命令,会显示当前系统时间日期
[root@hf-01 ~]# date
2018年 01月 14日 星期日 06:13:14 CST
[root@hf-01 ~]#
- date命令,在shell中用处非常大;对文件后缀增加一个时间,以便后期管理
- date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@hf-01 ~]# LANG=en 切换为英文显示
[root@hf-01 ~]# date
Sun Jan 14 06:19:49 CST 2018
[root@hf-01 ~]# date +%Y
2018 四位的年
[root@hf-01 ~]# date +%y
18 两位的年
[root@hf-01 ~]# date +%m
01 月份
[root@hf-01 ~]# date +%M
20 分钟
[root@hf-01 ~]# date +%d
14 日期
[root@hf-01 ~]# date +%D
01/14/18 直接标记年月日,不过格式比较特殊
[root@hf-01 ~]# date +%Y%m%d
20180114 年月日
[root@hf-01 ~]# date +%F
2018-01-14 年月日,这种带横杠的
[root@hf-01 ~]#
- 常见时间单位
[root@hf-01 ~]# date +%w
0 表示周几
[root@hf-01 ~]# date +%W
02 今年的第几周,今年的第二周
[root@hf-01 ~]# date +%h
Jan 英文的月份
[root@hf-01 ~]# date +%H
06 小时
[root@hf-01 ~]# date +%S
04 秒
[root@hf-01 ~]# date +%s
1515882702 这是一个时间戳,距离1970总共过去多少秒
- 时间其他标记方法
- date +%H:%M:%S = date +%T 时间
[root@hf-01 ~]# date +%T
06:24:36
[root@hf-01 ~]# date +%H:%M:%S
06:24:36
[root@hf-01 ~]#
- 显示日历 cal命令,查看到日期
[root@hf-01 ~]# cal
January 2018
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
[root@hf-01 ~]#
- 标记之前的日期
- 比如:在做nginx日志切割的时候,到了凌晨切割日志,到了零点零分切割的日志是前一天的日志。所以把日志加一个时间标记的话,应标记为昨天的日期
- 学会用date标记之前的日期
- day、month、year、hour、min后面可以加 s 可以不加 s
- 减号- 表示之前的日期,加号 + 表示从今往后的日期
- date -d "-1 day" +%F 显示前一天的日期
- date -d "-1 month" +%F 显示上个月的日期
- date -d "-1 years" +%F 显示上一年的日期
- date -d "+1 hour" +%T 显示下一小时
- date -d "+1 min" +%T 显示下一分钟
[root@hf-01 ~]# date -d "-1 day"
Sat Jan 13 06:47:30 CST 2018
[root@hf-01 ~]# date -d "-1 day" +%F
2018-01-13
[root@hf-01 ~]# date -d "+1 month" +%F
2018-02-14
[root@hf-01 ~]# date -d "+1 year" +%F
2019-01-14
[root@hf-01 ~]# date -d "+1 hour" +%T
08:09:05
[root@hf-01 ~]# date -d "+1 min" +%T
07:11:21
- 时间戳
- date +%s
- 另一种表现方法,表示时间戳
- date -d @1504620492 就是@后跟时间戳
[root@hf-01 ~]# date +%s
1515885248
[root@hf-01 ~]# date -d @1515885248
Sun Jan 14 07:14:08 CST 2018
[root@hf-01 ~]#
- 若想在linux系统中,把具体的日期换算成时间戳的时候,可以使用date +%s -d "2018-01-13 07:14:08"
[root@hf-01 ~]# date +%s -d "2018-01-13 07:14:08"
1515798848
[root@hf-01 ~]# date -d @1515798848
Sat Jan 13 07:14:08 CST 2018
[root@hf-01 ~]#
20.4 shell脚本中的变量
shell脚本中的变量
- 当脚本中使用某个字符串较频繁并且字符串长度很长时就应该使用变量代替
- 使用条件语句时,常使用变量 if [ $a -gt 1 ]; then ... ; fi
- 引用某个命令的结果时,用变量替代 n=
wc -l 1.txt
- 写和用户交互的脚本时,变量也是必不可少的 read -p "Input a number: " n; echo $n 如果没写这个n,可以直接使用$REPLY
- 内置变量 $0, $1, $2… $0表示脚本本身,$1 第一个参数,$2 第二个 .... $#表示参数个数
- 数学运算a=1;b=2; c=$(($a+$b))或者$[$a+$b]