|
20.1 shell脚本介绍
·shell是一种脚本语言
·可以使用逻辑判断、循环等语法
·可以自定义函数
·shell是系统命令的集合
·shell脚本可以实现自动化运维,能大大增加我们的运维效率
20.2 shell脚本结构和执行
·开头需要加 #!/bin/bash,在本机上可以运行,换台机器就不一定能执行。在行首文件头处指定接下来要运行的命令是通过哪一个解释器进行操作的。
·以#开头的行作为解释说明,脚本的名字以.sh结尾,用于区分这是一个shell脚本
·执行方法有两种:1,bash 或 sh sh文件 ;2,给文件加执行权限,然后 ./文件
1,bash 1.sh/bin/bash 实际上就是 /bin/sh
[root@localhost shell]# ll /bin/bash
-rwxr-xr-x. 1 root root 960472 8月 3 2017 /bin/bash
[root@localhost shell]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 12月 28 05:37 /bin/sh -> bash
2,有执行权限的话,chmod +x 1.sh; 可以直接 ./1.sh
3,其实直接输入文件的绝对路径也可以执行
[root@localhost shell]# /root/shell/01.sh
123
22:22:27 up 2:21, 1 user, load average: 0.02, 0.02, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.65.1 20:08 3.00s 0.06s 0.00s /bin/bash /root/shell/01.sh
总用量 4
-rwxr-xr-x 1 root root 32 4月 16 22:22 01.sh
·查看脚本执行过程:bash -x 1.sh / sh -x 1.sh
[root@localhost shell]# sh -x 01.sh
+ echo 123
123
+ w
22:23:55 up 2:23, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.65.1 20:08 3.00s 0.07s 0.00s sh -x 01.sh
+ ls -l
总用量 4
-rwxr-xr-x 1 root root 32 4月 16 22:22 01.sh
·查看脚本是否语法错误:bash -n 1.sh
故意写错脚本(随机的字符是不报错的)
[root@localhost shell]# sh -n 01.sh
01.sh:行10: 语法错误: 未预期的文件结尾
20.3 date命令用法
[root@localhost ~]# date
2018年 04月 17日 星期二 20:15:04 CST
·date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@localhost ~]# date +%Y-%m-%d
2018-04-17
[root@localhost ~]# date +%y-%m-%d
18-04-17
[root@localhost ~]# date +%d ## d 日期
17
[root@localhost ~]# date +%D ## 美国格式年月日
04/17/18
[root@localhost ~]# date +%M ## M 分钟
06
[root@localhost ~]# date +%m ## m 月份
04
[root@localhost ~]# date +%F ## 日期
2018-04-17
·date +%H:%M:%S = date +%T 时间
[root@localhost ~]# date +%H:%M:%S
21:29:15
[root@localhost ~]# date +%T
21:29:35
[root@localhost ~]# date +%H ##小时
21
[root@localhost ~]# date +%h ##月份(带格式,设置中文就是 4月,英文就是 Aprl)
4月
[root@localhost ~]# date +%M ##分钟
31
[root@localhost ~]# date +%m ##月份
04
[root@localhost ~]# date +%S ##秒
44
[root@localhost ~]# date +%s ##世时间戳
1523971907
·date +%s 时间戳
距离1970-01-01-00-00-00 过去多少秒
·date +%w, date +%W 星期
[root@localhost ~]# date +%w## 星期几
2
[root@localhost ~]# date +%W## 今年的第几周
16
·date -d @1504620492
[root@localhost ~]# date -d @1504620492##知道时间戳查日期
2017年 09月 05日 星期二 22:08:12 CST
[root@localhost ~]# date +%s -d "2018-4-17 21:52:30"##知道日期查时间戳
1523973150
·date -d "+1day" 一天后
[root@localhost ~]# date -d "+1day"
2018年 04月 18日 星期三 21:46:48 CST
[root@localhost ~]# date -d "+1day" +%F
2018-04-18
·date -d "-1 day" 一天前
·date -d "-1 month" 一月前 ##年year
[root@localhost ~]# date -d "-1 month" +%F
2018-03-17
·date -d "-1 min" 一分钟前 ##小时hour,秒second
[root@localhost ~]# date -d "-1 min" +%T
21:47:09
[root@localhost ~]# cal
四月 2018
日 一 二 三 四 五 六
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
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]
|
|
|