|
- 20.1 shell脚本介绍
- 20.2 shell脚本结构和执行
- 20.3 date命令用法
- 20.4 shell脚本中的变量
# 20.1 Shell脚本介绍
- shell是一种脚本语言 关注aming_linux blog.lishiming.net
- 可以使用逻辑判断、循环等语法
- 可以自定义函数
- shell是系统命令的集合
- shell脚本可以实现自动化运维,能大大增加我们的运维效率
# 20.2 Shell脚本结构和执行
- 开头需要加#!/bin/bahs //告诉系统,这个脚本是通过哪一个解释器来进行操作的
- 以#开头的行作为解释说明
- 脚本的名字以.sh结尾,用于区分这是一一个shell脚本
- 先创建一个shell目录
```
[root@aming-01 ~]# clear
[root@aming-01 ~]# mkdir shell
[root@aming-01 ~]# cd shell/
[root@aming-01 shell]# ls
[root@aming-01 shell]# vi 01.sh
#!/bin/bash
echo "123"
w
ls
~
~
:wq
[root@aming-01 shell]# vi 01.sh
```
- 执行方法有两种:
chmod +x 1.sh; ./1.sh
bash 1.sh , sh 1.sh
```
[root@aming-01 shell]# sh 01.sh
123
21:07:19 up 5 min, 2 users, load average: 0.04, 0.33, 0.20
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:02 4:39 0.02s 0.02s -bash
root pts/0 192.168.202.1 21:03 7.00s 0.03s 0.01s w
01.sh
[root@aming-01 shell]#
```
- 还有一种, 文件开头的意思就是意味着 接下来的命令是由这个文件来解析的。
```
[root@aming-01 shell]# chmod a+x 01.sh
[root@aming-01 shell]# ./01.sh
123
21:09:48 up 7 min, 2 users, load average: 0.14, 0.23, 0.18
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:02 7:08 0.02s 0.02s -bash
root pts/0 192.168.202.1 21:03 4.00s 0.03s 0.00s /bin/bash ./01.sh
01.sh
[root@aming-01 shell]#
[root@aming-01 shell]# cat 01.sh
#!/bin/bash
echo "123"
w
ls
[root@aming-01 shell]#
```
- bin/bash 其实就是 bin/sh
```
[root@aming-01 shell]# ls -l /bin/bash
-rwxr-xr-x. 1 root root 960392 8月 3 2016 /bin/bash
[root@aming-01 shell]# ls -l /bin/sh
lrwxrwxrwx. 1 root root 4 10月 5 22:18 /bin/sh -> bash
[root@aming-01 shell]#
```
- 查看脚本执行过程,每个+表示一个步骤
```
[root@aming-01 shell]# sh -x 01.sh
+ echo 123
123
+ w
21:17:01 up 14 min, 2 users, load average: 0.02, 0.07, 0.12
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 21:02 14:21 0.02s 0.02s -bash
root pts/0 192.168.202.1 21:03 5.00s 0.05s 0.00s sh -x 01.sh
+ ls
01.sh
[root@aming-01 shell]#
```
- 也可以用 sh -n 查看脚本的语法是否正确
- 没有输出,表示没有错误
```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]#
```
- 修改一下文件测试一下错误
```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]# vi 01.sh
#!/bin/bash
echo "123"
w
ls
for i in `seq 1 10`
do
echo $i
~
~
~
:wq
```
- 再执行下sh -n 看下
```
[root@aming-01 shell]# sh -n 01.sh
01.sh:行8: 语法错误: 未预期的文件结尾
[root@aming-01 shell]#
```
- 改回来
```
[root@aming-01 shell]# vi 01.sh
#!/bin/bash
echo "123"
w
ls
~
~
~
:wq
```
- 再监测就是没用问题
```
[root@aming-01 shell]# sh -n 01.sh
[root@aming-01 shell]#
```
# 20.3 date命令用法
- date 在shell中用处非常大;对文件后缀增加一个时间,以便后期管理
- date +%Y-%m-%d, date+%y-%m-d 年月日
```
[root@aming-01 ~]# date
2017年 11月 20日 星期一 22:01:01 CST
[root@aming-01 ~]#
```
- m 月,M 分钟
```
[root@aming-01 ~]# date
2017年 11月 20日 星期一 22:01:01 CST
[root@aming-01 ~]# date +%Y
2017
[root@aming-01 ~]# date +%y
17
[root@aming-01 ~]# date +%m
11
[root@aming-01 ~]# date +%M
27
[root@aming-01 ~]#
```
- 日期 d 大D
```
[root@aming-01 ~]# date +%d
20
[root@aming-01 ~]# date +%D
11/20/17
[root@aming-01 ~]#
```
- 日期,年月日 date +%Y%m%d
```
[root@aming-01 ~]# date +%Y%m%d
20171120
[root@aming-01 ~]#
```
- 日期 date +%F
```
[root@aming-01 ~]# date +%F
2017-11-20
[root@aming-01 ~]#
```
- 小时 ,分 ,秒
- s 表示一个时间戳 表示 距离1970年1月1日0点0分到现在的 总共多少秒
```
[root@aming-01 ~]# date +%H
22
[root@aming-01 ~]# date +%s
1511188397
[root@aming-01 ~]# date +%S
22
[root@aming-01 ~]# date +%S
32
[root@aming-01 ~]#
```
- 关于时间
- date +%T
- date +%H%M%S
```
[root@aming-01 ~]# date +%T
22:35:38
[root@aming-01 ~]# date +%H%M%S
223608
[root@aming-01 ~]#
```
- h显示月份,
```
[root@aming-01 ~]# date +%h
11月
[root@aming-01 ~]#
```
- 先把语言改成英文
```
[root@aming-01 ~]# LANG=en
[root@aming-01 ~]# date +%h
Nov
[root@aming-01 ~]#
```
- 加上冒号 date +%H:%M:%S 等同于 date +%T
```
[root@aming-01 ~]# date +%H:%M:%S
22:42:38
[root@aming-01 ~]# date +%T
22:42:42
[root@aming-01 ~]#
```
- 周
- w 周几
- W 今年的第几周
```
[root@aming-01 ~]# date +%w
1
[root@aming-01 ~]# date +%W
47
[root@aming-01 ~]#
```
- 显示日历cal
```
[root@aming-01 ~]# cal
November 2017
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
[root@aming-01 ~]#
```
- date -d "+1 day" +%F 一天后
- date -d "-1 day" +%F一天前
- date -d "-1 month" +%F一个月前
- date -d "-1 min " +%F一分钟前
- date -d "-1 year " +%F 一年前
- 标记昨天的日期 date -d "-1 day"
```
[root@aming-01 ~]# date
Mon Nov 20 23:07:38 CST 2017
[root@aming-01 ~]# date -d "-1 day"
Sun Nov 19 23:07:46 CST 2017
[root@aming-01 ~]#
[root@aming-01 ~]# date -d "-1 day" +%F
2017-11-19
[root@aming-01 ~]#
```
- 上个月 date -d "-1 month"
```
[root@aming-01 ~]# date -d "-1 month" +%F
2017-10-20
[root@aming-01 ~]#
```
- 上一年 date -d "-1 year"
```
[root@aming-01 ~]# date -d "-1 year" +%F
2016-11-20
[root@aming-01 ~]# date -d "-1 year"
Sun Nov 20 23:10:43 CST 2016
[root@aming-01 ~]#
```
- 上一个小时 date -d "-1 hour"
```
[root@aming-01 ~]# date -d "-1 hour" +%T
22:11:43
[root@aming-01 ~]#
```
- 时间戳 转换成具体的日期时间,也可以现在的时间转换成时间戳
```
[root@aming-01 ~]# date +%s
1511190759
[root@aming-01 ~]#
[root@aming-01 ~]# date -d @1511190759
Mon Nov 20 23:12:39 CST 2017
[root@aming-01 ~]#
[root@aming-01 ~]# date +%s -d "2017-11-20 23:12:39"
1511190759
[root@aming-01 ~]#
```
# 20.4 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]
|
|
|