博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux基础(day69)
阅读量:7091 次
发布时间:2019-06-28

本文共 5489 字,大约阅读时间需要 18 分钟。

hot3.png

20.1shell脚本介绍

shell脚本介绍

  • shell是一种脚本语言 和传统的开发语言比较,会比较简单
    • shell有自己的语法;可以使用逻辑判断、循环等语法
  • 可以自定义函数,目的就是为了减少重复的代码
  • shell是系统命令的集合
  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

20.2 shell脚本结构和执行

shell脚本结构和执行

  • 开头需要加#!/bin/bash
  • 以#开头的行作为解释说明
  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本
  • 执行方法有两种
  • chmod +x 1.sh; ./1.sh
  • bash 1.sh
  • 查看脚本执行过程 bash -x 1.sh
  • 查看脚本是否语法错误 bash -n 1.sh

shell脚本结构和执行

  1. 创建一个shell文件夹,并切入到文件夹中
[root@hanfeng ~]# mkdir shell[root@hanfeng ~]# cd shell/[root@hanfeng shell]#
  1. 写shell脚本
  • #! /bin/bash //第一行必须这么写,固定格式,作用就是脚本若是在当台机器上去执行,可以不加这一行也没关系,因为它知道下面若干条的命令能在这台机器上去执行,去解析
[root@hanfeng shell]# vi 01.sh#! /bin/bash   	//固定格式echo "123"wls保存退出
  1. 执行脚本——>sh 01.sh
[root@hanfeng shell]# sh 01.sh123 22:45:12 up 14 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    0.00s  0.05s  0.03s w01.sh[root@hanfeng shell]#
  1. 在当前终端里,把01.sh中的#! /bin/bash 去掉后在执行脚本,会看到得到的结果相同,不会出现任何的问题,这就说明这台机器是能识别里面一条一条的命令的,去运行这里面的命令;但若是换一台机器,就不一定能执行了
  2. 在第一行,文件头指定 #!/bin/bash ,接下来要运行的命令是通过哪一个解释器来操作的,通常都是 /bin/bash 解释器来执行的
  3. 给01.sh文件一个执行权限
[root@hanfeng shell]# chmod a+x 01.sh[root@hanfeng shell]#

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

[root@hanfeng shell]# ./01.sh123 23:11:21 up 41 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    1.00s  0.05s  0.00s /bin/bash ./01.sh01.sh[root@hanfeng shell]#
  1. /bin/bash也是一条命令, /bin/bash 和 /bin/sh 是同一个文件
[root@hanfeng shell]# ls -l /bin/bash-rwxr-xr-x. 1 root root 960368 6月  10 2014 /bin/bash[root@hanfeng shell]# ls -l /bin/shlrwxrwxrwx. 1 root root 4 10月 21 00:47 /bin/sh -> bash[root@hanfeng shell]#
  1. 01.sh文件内容就是被/bin/bash所解析的
  2. 若没有 /bin/bash ,可以使用 /bin/bash 01.sh去执行
[root@hanfeng shell]# /bin/bash 01.sh123 23:21:43 up 51 min,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    7.00s  0.03s  0.00s w01.sh[root@hanfeng shell]#
  1. 若是在shell脚本中在写入第二遍 #号开头的行, 就表示解释说明的作用
  2. 脚本的一般都是以.sh结尾,是为了区分这是一个shell脚本,否则需要打开这个文件,才知道是shell脚本
  3. 运行shell脚本有两种方法
  • 一种是sh 01.sh运行shell脚本
  • 另一种方法
    • 先 chmod a+x 1.sh 给文件加一个执行权限
    • 再 ./1.sh 去执行
      • 这里的 ./ 就相当于一个相对路径,相对当前一个路径
      • 也可以使用绝对路径去执行脚本 /root/shell/01.sh ,其实就是找到这个文件去执行
[root@hanfeng shell]# /root/shell/01.sh123 23:45:02 up  1:14,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    6.00s  0.03s  0.00s w01.sh[root@hanfeng shell]#
  1. 查看脚本执行过程 sh -x 01.sh 或者bash -x 01.sh
  • -x,就是显示脚本执行的过程
  • 每一个加号,表示一个操作步骤
[root@hanfeng shell]# sh -x 01.sh+ echo 123123+ w 23:47:35 up  1:17,  1 user,  load average: 0.00, 0.01, 0.05USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHATroot     pts/0     22:32    7.00s  0.03s  0.00s sh -x 01.sh+ ls01.sh[root@hanfeng shell]#
  1. 查看脚本是否有错误 sh -n 01.sh
  • 若是没有任何的输出,那么脚本则没有错误
  • sh -n 01.sh命令是检测是否存在语法错误
[root@hanfeng shell]# sh -n 01.sh[root@hanfeng shell]#

20.3 date命令用法

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命令用法

  1. date命令,会显示当前系统时间日期
[root@hf-01 ~]# date2018年 01月 14日 星期日 06:13:14 CST[root@hf-01 ~]#
  1. date命令,在shell中用处非常大;对文件后缀增加一个时间,以便后期管理
  2. date +%Y-%m-%d, date +%y-%m-%d 年月日
[root@hf-01 ~]# LANG=en	切换为英文显示[root@hf-01 ~]# dateSun Jan 14 06:19:49 CST 2018[root@hf-01 ~]# date +%Y	2018	四位的年[root@hf-01 ~]# date +%y18		两位的年[root@hf-01 ~]# date +%m01		月份[root@hf-01 ~]# date +%M20		分钟[root@hf-01 ~]# date +%d14		日期[root@hf-01 ~]# date +%D01/14/18	直接标记年月日,不过格式比较特殊[root@hf-01 ~]# date +%Y%m%d20180114	年月日[root@hf-01 ~]# date +%F2018-01-14	年月日,这种带横杠的[root@hf-01 ~]#
  1. 常见时间单位
[root@hf-01 ~]# date +%w0		表示周几[root@hf-01 ~]# date +%W02		今年的第几周,今年的第二周[root@hf-01 ~]# date +%hJan		英文的月份[root@hf-01 ~]# date +%H06			小时[root@hf-01 ~]# date +%S04			秒[root@hf-01 ~]# date +%s1515882702	这是一个时间戳,距离1970总共过去多少秒
  1. 时间其他标记方法
  • date +%H:%M:%S = date +%T 时间
[root@hf-01 ~]# date +%T06:24:36[root@hf-01 ~]# date +%H:%M:%S06:24:36[root@hf-01 ~]#
  1. 显示日历 cal命令,查看到日期
[root@hf-01 ~]# cal    January 2018    Su Mo Tu We Th Fr Sa    1  2  3  4  5  6 7  8  9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728 29 30 31[root@hf-01 ~]#
  1. 标记之前的日期
  • 比如:在做nginx日志切割的时候,到了凌晨切割日志,到了零点零分切割的日志是前一天的日志。所以把日志加一个时间标记的话,应标记为昨天的日期
  1. 学会用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@hf-01 ~]# date -d "-1 day"Sat Jan 13 06:47:30 CST 2018[root@hf-01 ~]# date -d "-1 day" +%F2018-01-13[root@hf-01 ~]# date -d "+1 month" +%F2018-02-14[root@hf-01 ~]# date -d "+1 year" +%F2019-01-14[root@hf-01 ~]# date -d "+1 hour" +%T08:09:05[root@hf-01 ~]# date -d "+1 min" +%T07:11:21
  1. 时间戳
  • date +%s
  • 另一种表现方法,表示时间戳
    • date -d @1504620492 就是@后跟时间戳
[root@hf-01 ~]# date +%s1515885248[root@hf-01 ~]# date -d @1515885248Sun Jan 14 07:14:08 CST 2018[root@hf-01 ~]#
  1. 若想在linux系统中,把具体的日期换算成时间戳的时候,可以使用date +%s -d "2018-01-13 07:14:08"
[root@hf-01 ~]# date +%s -d "2018-01-13 07:14:08"1515798848[root@hf-01 ~]# date -d @1515798848Sat Jan 13 07:14:08 CST 2018[root@hf-01 ~]#

20.4 shell脚本中的变量

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]

转载于:https://my.oschina.net/u/3707314/blog/1617205

你可能感兴趣的文章
分块矩阵和行列式
查看>>
陶哲轩实分析引理8.4.5
查看>>
WINCE 下载地址(转)
查看>>
Linux 小知识翻译 - 「单CD 的linux」
查看>>
Linux 小知识翻译 - 「RFC」
查看>>
20145234黄斐《信息安全系统设计基础》期中总结
查看>>
STM32F103 强制转换
查看>>
ANGULAR 开发用户选择器指令
查看>>
Java String编码转换(转自http://blog.csdn.net/okman1214/article/details/4397772)
查看>>
css3-巧用选择器 “:target”
查看>>
JPEG最优压缩参数试验【光影魔术手VS Image Optimizer】
查看>>
accp
查看>>
单例模式
查看>>
html02表格的使用
查看>>
【待续】【HTML5】用Canvas标签创建第一张条线图
查看>>
zookeeper之 zkServer.sh命令、zkCli.sh命令、四字命令
查看>>
Hbase shell 常用命令
查看>>
oracle之 v$sql_monitor 监视正在运行的SQL语句的统计信息
查看>>
SEO之优化代码
查看>>
【BZOJ4025】 二分图(线段树分治)
查看>>