shell基础

1.shell开头需要指定解释器,例如#!/bin/bash,指定解释器的路径为/bin/bash,并让解释器得以从头解释脚本文件并执行脚本中的命令。

[root@localhost test_shell]# vim test.sh
#!/bin/bash
echo "123"

2.文件名通常以.sh结尾,虽然linux系统中后缀名与文件类型并无关,但是这样的命名规则可以让管理员更好地识别脚本的类别。

3.使用-x选项查看脚本的执行过程

[root@localhost test_shell]# bash -x test.sh
+ echo 123
123

4.使用-n选项来判断shell脚本是否有语法错误,如果没有错误则没有任何输出

5.date命令用法(date常用于对脚本执行产生的记录文件等添加时间信息)

①直接执行date得到时间日期信息

[root@localhost test_shell]# date
Wed Feb  7 14:19:03 CST 2018

②附加选项获取年、月、日等信息

[root@localhost test_shell]# date
Wed Feb  7 14:19:03 CST 2018
[root@localhost test_shell]# date +%Y        ⇒获取年份
2018
[root@localhost test_shell]# date +%y        ⇒获取年份
18
[root@localhost test_shell]# date +%m        ⇒获取月份
02
[root@localhost test_shell]# date +%M        ⇒获取分钟
23
[root@localhost  test_shell]#  date  +%H                ⇒获取小时数
14
[root@localhost  test_shell]#  date  +%h                ⇒获取月份(英文简称)
Feb
[root@localhost  test_shell]#  date  +%s                ⇒获取时间戳(距离1970/1/1的秒数)
1517985022
[root@localhost  test_shell]#  date  +%S                ⇒获取秒数
29
[root@localhost  test_shell]#  date  +%T                ⇒获取时间
14:33:25
[root@localhost  test_shell]#  date  +%H:%M:%S          ⇒获取时间(与%T效果一致)
14:34:29
[root@localhost  test_shell]#  date  +%w                ⇒获取星期几
3
[root@localhost  test_shell]#  date  +%W                ⇒获取本年内周数(年内第n周)
06
[root@localhost test_shell]# date +%d        ⇒获取日期
07
[root@localhost test_shell]# date +%D        ⇒获取日期(以 月/日/年 格式排列)
02/07/18
[root@localhost test_shell]# date +%Y%m%d        ⇒获取日期(以 年月日 格式排列,无分隔字符)
20180207
[root@localhost test_shell]# date +%F                ⇒获取日期(以 年-月-日 格式排列)
2018-02-07

③使用-d选项来获取过去的时间

[root@localhost test_shell]# date -d "-1 day" +%F       ⇒显示前一天的日期
2018-02-06
[root@localhost test_shell]# date -d "-1 year" +%F      ⇒显示前一年的日期
2017-02-07
[root@localhost test_shell]# date -d "-1 month" +%F     ⇒显示前一个月的日期
2018-01-07
[root@localhost test_shell]# date -d "-1 minute" +%T    ⇒显示前一分钟的时间
14:38:41
[root@localhost test_shell]# date -d "-1 hour" +%T      ⇒显示前一小时的时间
13:39:58
[root@localhost test_shell]#

④时间戳转换

[root@localhost test_shell]# date +%s -d "2018-02-07 14:34:29"
1517985269
[root@localhost test_shell]# date -d @1517985022
Wed Feb  7 14:30:22 CST 2018




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章