您的位置:首页 > 运维架构 > Shell

shell脚本介绍shell脚本结构和执行date命令用法shell脚本中的变量

2018-09-14 10:02 1051 查看

shell脚本介绍

shell是一种脚本语言 和传统的开发语言比较,会比较简单

shell有自己的语法;可以使用逻辑判断、循环等语法

可以自定义函数,目的就是为了减少重复的代码

shell是系统命令的集合

shell脚本可以实现自动化运维,能大大增加我们的运维效率

shell脚本结构和执行

开头需要加#!/bin/bash

以#开头的行作为解释说明

脚本的名字以.sh结尾,用于区分这是一个shell脚本

执行方法有两种

chmod +x 1.sh; ./1.sh

bash 1.sh

查看脚本执行过程 bash -x 1.sh

查看脚本是否语法错误 bash -n 1.sh

实践操作:

创建一个shell文件夹,并切入到文件夹中

[root@yong-01 ~]# mkdir shell
[root@yong-01 ~]# cd shell/
[root@yong-01 shell]#


写shell脚本

#! /bin/bash //第一行必须这么写,固定格式,作用就是脚本若是在当台机器上去执行,可以不加这一行也没关系,因为它知道下面若干条的命令能在这台机器上去执行,去解析

[root@yong-01 shell]# vim 1.sh

#!/bin/bash
echo "abc"
w
ls


执行脚本——>sh 1.sh

[root@yong-01 shell]# sh 1.sh
abc
22:52:59 up 2 min,  1 user,  load average: 0.19, 0.22, 0.09
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.1    22:50    3.00s  0.06s  0.00s sh 1.sh
1.sh


在当前终端里,把1.sh中的#! /bin/bash 去掉后在执行脚本,会看到得到的结果相同,不会出现任何的问题,这就说明这台机器是能识别里面一条一条的命令的,去运行这里面的命令;但若是换一台机器,就不一定能执行了

在第一行,文件头指定 #!/bin/bash ,接下来要运行的命令是通过哪一个解释器来操作的,通常都是 /bin/bash 解释器来执行的

给1.sh文件一个执行权限 chmod a+x 1.sh

[root@yong-01 shell]# chmod a+x 1.sh


执行shell脚本 ./1.sh 能这样执行,说明这些命令被解析了,被/bin/bash认识了

[root@yong-01 shell]# ./1.sh
abc
22:56:26 up 5 min,  1 user,  load average: 0.01, 0.11, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.1    22:50    2.00s  0.06s  0.00s /bin/bash ./1.sh
1.sh


/bin/bash也是一条命令, /bin/bash 和 /bin/sh 是同一个文件

[root@yong-01 shell]# ll /bin/bash
-rwxr-xr-x. 1 root root 960472 8月   3 2017 /bin/bash
[root@yong-01 shell]# ll /bin/sh
lrwxrwxrwx. 1 root root 4 3月   6 20:09 /bin/sh -> bash


1.sh文件内容就是被/bin/bash所解析的

若没有 /bin/bash ,可以使用 /bin/bash 1.sh去执行

[root@yong-01 shell]# /bin/bash 1.sh
abc
22:57:50 up 7 min,  1 user,  load average: 0.00, 0.08, 0.07
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.1    22:50    6.00s  0.10s  0.00s w
1.sh


若是在shell脚本中在写入第二遍 #号开头的行, 就表示解释说明的作用

脚本的一般都是以.sh结尾,是为了区分这是一个shell脚本,否则需要打开这个文件,才知道是shell脚本

运行shell脚本有两种方法

一种是sh 1.sh运行shell脚本

另一种方法

先 chmod a+x 1.sh 给文件加一个执行权限

再 ./1.sh 去执行

这里的 ./ 就相当于一个相对路径,相对当前一个路径

也可以使用绝对路径去执行脚本 /root/shell/1.sh ,其实就是找到这个文件去执行

[root@yong-01 shell]# /root/shell/1.sh
abc
22:58:22 up 7 min,  1 user,  load average: 0.00, 0.07, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.1    22:50    6.00s  0.10s  0.00s /bin/bash /root/shell/1.sh
1.sh


查看脚本执行过程 sh -x 1.sh 或者bash -x 1.sh

-x,就是显示脚本执行的过程

每一个加号,表示一个操作步骤

[root@yong-01 shell]# sh -x 1.sh
+ echo abc
abc
+ w
22:58:45 up 8 min,  1 user,  load average: 0.00, 0.07, 0.06
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.180.1    22:50    5.00s  0.11s  0.00s w
+ ls
1.sh


查看脚本是否有错误 sh -n 1.sh

若是没有任何的输出,那么脚本则没有错误

sh -n 1.sh命令是检测是否存在语法错误

[root@yong-01 shell]# sh -n 1.sh


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@yong-01 ~]# date
2018年 07月 11日 星期三 23:00:25 CST


date命令,在shell中用处非常大;对文件后缀增加一个时间,以便后期管理

date +%Y-%m-%d, date +%y-%m-%d 年月日

[root@yong-01 ~]# LANG=en	切换为英文显示
[root@yong-01 ~]# date
Wed Jul 11 23:01:05 CST 2018
[root@yong-01 ~]# date +%Y
2018	四位的年
[root@yong-01 ~]# date +%y
18		两位的年
[root@yong-01 ~]# date +%m
07		月份
[root@yong-01 ~]# date +%M
02		分钟
[root@yong-01 ~]# date +%d
11   	日期
[root@yong-01 ~]# date +%D
07/11/18	直接标记年月日,不过格式比较特殊
[root@yong-01 ~]# date +%Y%m%d
20180711	年月日
[root@yong-01 ~]# date +%F
2018-07-11	年月日,这种带横杠的


常见时间单位

[root@yong-01 ~]# date +%w
3		表示周几
[root@yong-01 ~]# date +%W
28		今年的第几周,今年的第28周
[root@yong-01 ~]# date +%h
Jul		英文的月份
[root@yong-01 ~]# date +%H
23			小时
[root@yong-01 ~]# date +%S
53			秒
[root@yong-01 ~]# date +%s
1531322044	这是一个时间戳,距离1970总共过去多少秒


时间其他标记方法

date +%H:%M:%S = date +%T 时间

[root@yong-01 ~]# date +%T
23:14:29
[root@yong-01 ~]# date +%H:%M:%S
23:14:57


显示日历 cal命令,查看到日期

[root@yong-01 ~]# cal
July 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


标记之前的日期

比如:在做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@yong-01 ~]# date -d "-1 day"
Tue Jul 10 23:16:11 CST 2018
[root@yong-01 ~]# date -d "-1 day" +%F
2018-07-10
[root@yong-01 ~]# date -d "+1 month" +%F
2018-08-11
[root@yong-01 ~]# date -d "+1 year" +%F
2019-07-11
[root@yong-01 ~]# date -d "-1 hour" +%T
22:17:14
[root@yong-01 ~]# date -d "-1 min" +%T
23:16:21


时间戳 date +%s

另一种表现方法,表示时间戳

date -d @1531322291就是@后跟时间戳

[root@yong-01 ~]# date +%s
1531322291
[root@yong-01 ~]# date -d @1531322291
Wed Jul 11 23:18:11 CST 2018


若想在linux系统中,把具体的日期换算成时间戳的时候,可以使用date +%s -d "2018-07-11 23:18:11"

[root@yong-01 ~]# date +%s -d "2018-07-11 23:18:11"
1531322291
[root@yong-01 ~]# date -d @1531322291
Wed Jul 11 23:18:11 CST 2018


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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Bash GT
相关文章推荐