[root@test01 shell]# vi 01.sh
#!/bin/bash #固定开头格式
#written by sc #固定开头格式,意思是谁写的
#2019-06-17 #固定开头格式,意思是年月日
echo "123" #打印 123
w #查看负载
ls #查看当前目录
执行01.sh示例
[root@test01 shell]# sh 01.sh
12319:46:25 up 2:41,2 users, load average:0.05,0.03,0.05USERTTYFROMLOGIN@ IDLEJCPUPCPUWHAT
root pts/0192.168.85.116:472:16m 0.07s 0.01s vi 01.sh
root pts/1192.168.85.119:431.00s 0.11s 0.00s sh 01.sh
01.sh
执行脚本有两种方法
方法一:
/bin/sh实际是bash的软连接;其实是真正执行的是bash
[root@test01 shell]# ls -l /bin/bash
-rwxr-xr-x.1 root root 964608 Oct 312018/bin/bash
[root@test01 shell]# ls -l /bin/sh
lrwxrwxrwx.1 root root 4 Mar 1907:53/bin/sh -> bash
[root@test01 shell]# bash 01.sh
12309:46:46 up 39 min,1 user, load average:0.30,0.16,0.10USERTTYFROMLOGIN@ IDLEJCPUPCPUWHAT
root pts/0192.168.85.109:336.00s 0.10s 0.00s w
01.sh
方法二:
[root@test01 shell]# chmod a+x 01.sh #增加执行权限
[root@test01 shell]# ./01.sh #可执行脚本
12322:03:55 up 4:59,1 user, load average:0.02,0.04,0.05USERTTYFROMLOGIN@ IDLEJCPUPCPUWHAT
root pts/1192.168.85.119:433.00s 0.09s 0.00s /bin/bash ./01.sh
01.sh
查看脚本执行过程:bash -x 脚本名称
[root@test01 shell]# sh -x 01.sh
+ echo 123123+ w
09:40:43 up 33 min,1 user, load average:0.00,0.02,0.06USERTTYFROMLOGIN@ IDLEJCPUPCPUWHAT
root pts/0192.168.85.109:333.00s 0.08s 0.01s w
+ ls
01.sh
查看脚本是否语法错误: bash -n 脚本名称
[root@test01 shell]# vim 01.sh
#!/bin/bash
echo "123 #注意:为了测试 bash -n 把123右侧的双引号去掉
w
ls
[root@test01 shell]# sh -n 01.sh #执行脚本
01.sh: line 2: unexpected EOFwhile looking for matching `"' #检查完以后显示语法是错的
01.sh: line 5: syntax error: unexpected end of file
3. date命令用法
时间格式用法
时间格式举例:
[root@test01 shell]# date +%Y #大写Y 年份20192019[root@test01 shell]# date +%y #小写y 简写的年 年份1919[root@test01 shell]# date +%m #小写m 月份0606[root@test01 shell]# date +%M #分钟2222[root@test01 shell]# date +%d #日期1616[root@test01 shell]# date +%D #年月日2019061606/16/19[root@test01 shell]# date +%F #日期格式2019-06-162019-06-16[root@test01 shell]# date +%H #大写H 小时1111[root@test01 shell]# date +%h #小写h 英文的月
Jul
[root@test01 shell]# date +%S #大写S 秒5454[root@test01 shell]# date +%T=date +%H:%M:%S #常规时间11:42:4011:42:40[root@test01 shell]# date +%w #星期几00[root@test01 shell]# date +%W #今天是今年的第23周
23
日历:
[root@test01 shell]# cal #显示当月日历
June 2019
Su Mo Tu We Th Fr Sa
123456789101112131415161718192021222324252627282930[root@test01 TD]# cal 112019 #显示指定月年
November 2019
Su Mo Tu We Th Fr Sa
123456789101112131415161718192021222324252627282930
时间戳举例:
[root@test01 shell]# date +%s #小写s 时间戳15606564451560656445[root@test01 shell]# date -d @1560656445
Sun Jun 1611:40:45CST2019[root@test01 shell]# date +%s -d "2019-06-16 11:40:45"1560656445
date -d选项用法
-1,+1 年月日举例:
[root@test01 shell]# date -d "-1 day"+%F #一天前
2019-06-15[root@test01 shell]# date -d "+1 day"+%F #一天后
2019-06-17[root@test01 shell]# date -d "-1 week"+%F #一周前
2019-11-11[root@test01 shell]# date -d "+1 week"+%F #一周后
2019-11-25[root@test01 shell]# date -d "-1 month"+%F #一月前
2019-05-16[root@test01 shell]# date -d "+1 month"+%F #一月后
2019-07-16[root@test01 shell]# date -d "-1 year"+%F #一年前
2018-06-16[root@test01 shell]# date -d "+1 year"+%F #一年后
2020-06-16-1,+1 小时分钟举例:
[root@test01 shell]# date -d "-1 hour"+%T #一小时前
11