shell脚本介绍

  • shell是一种脚本语言 aming_linux(公众号) (博客)
  • 可以使用逻辑判断、循环等语法
  • 可以自定义函数
  • shell是系统命令的集合
  • shell脚本可以实现自动化运维,能大大增加我们的运维效率

shell脚本结构和执行

  • 开头需要加#!/bin/bash,意味着接下来的语句是由这个文件解析的,因为有了它我们才可以./1.sh这样执行,不然只能/bin/bash 1.sh这样执行
[root@akuilinux01 shell]# chmod a+x 1.sh 
[root@akuilinux01 shell]# ./1.sh
123
 22:37:09 up  4:02,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.21.1     22:29    5.00s  0.10s  0.00s /bin/bash ./1.sh
root     pts/1    192.168.21.1     19:00    3:31m  0.08s  0.08s -bash
1.sh
[root@akuilinux01 shell]# /bin/bash 1.sh 
123
 22:37:27 up  4:03,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.21.1     22:29    7.00s  0.10s  0.00s /bin/bash 1.sh
root     pts/1    192.168.21.1     19:00    3:31m  0.08s  0.08s -bash
1.sh
  • 以#开头的行作为解释说明,除了第一行的#!/bin/bash和有些特殊的脚本
  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本
  • 执行方法有两种
    • chmod +x 1.sh; ./1.sh
    • sh 1.sh
    • sh -x 1.sh 显示执行过程
    [root@akuilinux01 shell]# sh -x 1.sh 
    + echo 123
    123
    + w
    22:43:05 up  4:08,  2 users,  load average: 0.00, 0.01, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:29    1.00s  0.11s  0.00s sh -x 1.sh
    root     pts/1    192.168.21.1     19:00    3:37m  0.08s  0.08s -bash
    + ls
    1.sh
    带+号的就是执行的语句
    
    • sh -n 1.sh 可以检查脚本的语法问题

date命令用法

  • 年月日
[root@akuilinux01 shell]# date +%Y-%m-%d
2018-07-11
[root@akuilinux01 shell]# date +%y-%m-%d
18-07-11
[root@akuilinux01 shell]# date +%Y%m%d
20180711
[root@akuilinux01 shell]# date +%D
07/11/18
[root@akuilinux01 shell]# date +%F
2018-07-11
  • date +%s  时间戳,距离1970年1月1日0点0分过去多少秒,还有时间戳和时间之间转换
[root@akuilinux01 shell]# date +%s
1531320917
[root@akuilinux01 shell]# date -d @1531321618
2018年 07月 11日 星期三 23:06:58 CST
[root@akuilinux01 shell]# date +%s -d "2018-07-10 23:06:58"
1531235218
  • 时间
[root@akuilinux01 shell]# date +%H:%M:%S
22:57:20
[root@akuilinux01 shell]# date +%T
22:57:32
  • 星期
[root@akuilinux01 shell]# date +%w
3
[root@akuilinux01 shell]# date +%W
28
W是今年的第几周
  • 日志标记昨天日期的方法
[root@akuilinux01 shell]# date -d "-1 day" +%F
2018-07-10
[root@akuilinux01 shell]# date -d "-1 month" +%F
2018-06-11
[root@akuilinux01 shell]# date -d "-1 year" +%F
2017-07-11
[root@akuilinux01 shell]# date -d "-1 hour" +%T
22:05:55
[root@akuilinux01 shell]# date -d "+1 day" +%F
2018-07-12
  • 查看日历
[root@akuilinux01 shell]# cal 
      七月 2018     
日 一 二 三 四 五 六
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

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]