shell脚本基础

shell简介:shell是一种脚本语言,可以使用逻辑判断、循环等语法,可以自定义函数,是系统命令的集合

shell脚本结构和执行方法
1.shell脚本开头需要加#!/bin/bash
2.以#开头的行作为注释
3.脚本的名字以.sh结尾,用于区分这是一个shell脚本
4.执行方法有两种:
1)bash test.sh
2)./test.sh
#第二种执行方式需要有执行权限(chmod +x test.sh)
5.查看脚本执行过程:bash -x test.sh
6.查看脚本是否语法错误:bash -n test.sh

#bash可以使用sh命令代替(在/usr/bin目录下,sh是bash的软连接文件)

date命令的用法
年月日:
[root@linux ~]# date
2019年 11月 19日 星期二 16:20:04 CST
[root@linux ~]# date +%Y-%m-%d
2019-11-19
[root@linux ~]# date +%Y
2019
[root@linux ~]# date +%m
11
[root@linux ~]# date +%d
19
[root@linux ~]# date +%y    #表示2019年,忽略20
19
[root@linux ~]# date +%F
2019-11-19

时分秒:

[root@linux ~]# date
2019年 11月 19日 星期二 16:27:46 CST
[root@linux ~]# date +%H-%M-%S
16-28-04
[root@linux ~]# date +%T
16:28:09
周:

[root@linux ~]# date +%w    #表示星期几
2
[root@linux ~]# date +%W    表示今年的第多少周
46
时间戳:

[root@linux ~]# date +%s    #表示1970年1月1日到现在
1574152197
[root@linux ~]# date -d @1534150197        #倒推时间戳
2018年 08月 13日 星期一 16:49:57 CST
补充:

[root@linux ~]# date
2019年 11月 19日 星期二 16:34:37 CST

[root@linux ~]# date -d "+3day"            #三天后
2019年 11月 22日 星期五 16:34:45 CST

[root@linux ~]# date -d "-3day" +%F        #三天前
2019-11-16

[root@linux ~]# date -d "-3 month" +%F        #三个月前
2019-08-19

[root@linux ~]# date -d "-30 min" +%F_%T    #30分钟前
2019-11-19_16:06:16

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值