加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell基础

发布时间:2020-12-15 17:04:41 所属栏目:安全 来源:网络整理
导读:1.shell开头需要指定解释器,例如#!/bin/bash,指定解释器的路径为/bin/bash,并让解释器得以从头解释脚本文件并执行脚本中的命令。 [root@localhosttest_shell]#vimtest.sh#!/bin/bashecho"123" 2.文件名通常以.sh结尾,虽然linux系统中后缀名与文件类型并

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

[root@localhosttest_shell]#vimtest.sh
#!/bin/bash
echo"123"

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

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

[root@localhosttest_shell]#bash-xtest.sh
+echo123
123

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

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

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

[root@localhosttest_shell]#date
WedFeb714:19:03CST2018

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

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

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

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

④时间戳转换

[root@localhosttest_shell]#date+%s-d"2018-02-0714:34:29"
1517985269
[root@localhosttest_shell]#date-d@1517985022
WedFeb714:30:22CST2018

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读