资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

3shell编程知识-创新互联

[TOC]

创新互联坚信:善待客户,将会成为终身客户。我们能坚持多年,是因为我们一直可值得信赖。我们从不忽悠初访客户,我们用心做好本职工作,不忘初心,方得始终。10年网站建设经验创新互联是成都老牌网站营销服务商,为您提供做网站、成都网站制作、网站设计、成都h5网站建设、网站制作、品牌网站建设、成都微信小程序服务,给众多知名企业提供过好品质的建站服务。

一,DAY6

1.shell脚本介绍

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

2.shell脚本结构和执行

  • 开头需要加#!/bin/bash,这是shell的固有格式,指定接下来要运行的命令,是通过那一个解释器来操作的
  • 以#开头的行作为解释说明,某些启动脚本#号开头的行有特定作用
    #! /bin/bash
    # chkconfig: 2345 10 90    定义启动级别,
    # description: Activates/Deactivates all network interfaces configured to \    描述信息  这两行一定要,没有这两行就不能添加到chkconfig列表里去
    #              start at boot time.  
    #
  • 脚本的名字以.sh结尾,用于区分这是一个shell脚本
  • 执行方法有两种:
    • chmod +x 1.sh; ./1.sh
    • bash 1.sh
    • 执行脚本用bash或sh都可以
      [root@mydb1 test]# ls -l /bin/sh 
      lrwxrwxrwx 1 root root 4 Apr 24  2019 /bin/sh -> bash
      [root@mydb1 test]# ls -l /bin/bash 
      -rwxr-xr-x 1 root root 906568 Mar 23  2017 /bin/bash
  • 查看脚本执行过程 bash -x 1.sh
  • 查看脚本是否语法错误bash -n 1.sh 只能检查语法上的错误

3.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 星期

    • 演示:

      [root@mydb1 test]# date +%y  简写年
      19
      [root@mydb1 test]# date +%Y  
      2019
      [root@mydb1 test]# date +%m  月份
      11
      [root@mydb1 test]# date +%M  分
      40
      [root@mydb1 test]# date +%d  天
      20
      [root@mydb1 test]# date +%D   月/日/年
      11/20/19
      [root@mydb1 test]# date +%Y%m%d 年月日 
      20191120
      [root@mydb1 test]# date +%F    年-月-日
      2019-11-20
      [root@mydb1 test]# date +%H    时
      11
      [root@mydb1 test]# date +%s   时间戳,距离197001010000到现在过去多少秒
      1574221403
      [root@mydb1 test]# date +%S   秒
      30
      [root@mydb1 test]# date +%T   时:分:秒
      11:49:36
      [root@mydb1 test]# date +%h  英文月份
      Nov
      [root@mydb1 test]# date +%H:%M:%S   等同于T
      11:52:40
      [root@mydb1 test]# date +%w   星期几
      3
      [root@mydb1 test]# date +%W    一年中第几周
      46
      
          [root@mydb1 test]# cal   日历形式
          November 2019   
          Su Mo Tu We Th Fr Sa
                                          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
      
          [root@mydb1 ~]# date +'%Y%m%d' -d '1 days ago'
          20191119
          [root@mydb1 ~]# date +'%Y%m%d' -d '-1 days'
          20191119
      
          [root@mydb1 ~]# date +%F -d '1 year' 1年以后
          2020-11-20
          [root@mydb1 ~]# date +%F -d '-1 year' 1年以前
          2018-11-20
      
          [root@mydb1 ~]# date +%T
          13:55:03
          [root@mydb1 ~]# date +%T -d '-1 hour'  1小时以前
          12:55:18
      
          [root@mydb1 ~]# date +%s
          1574229561
          [root@mydb1 ~]# date -d @1574229561   将时间戳转换为具体日期
          Wed Nov 20 13:59:21 CST 2019
      
          [root@mydb1 ~]# date +%s -d "2019-11-20 14:01:01"  将具体时间转换为时间戳
          1574229661

4.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]

5.shell脚本中的逻辑判断

  • 格式1:if 条件 ; then 语句; fi
    #!/bin/bash
    a=5
    if [ $a -gt 3 ]
    then
       echo ok
    fi
  • 格式2:if 条件; then 语句; else 语句; fi
    #!/bin/bash
    a=2
    if [ $a -gt 3 ]
    then
       echo ok
    else
       echo nook
    fi
  • 格式3:if …; then … ;elif …; then …; else …; fi
    #!/bin/bash
    a=3
    if [ $a -gt 4 ]
    then
       echo ">1"
    elif [ $a -lt 6 ]
    then
       echo "<6 && >1"
    else
       echo nook
    fi
  • 逻辑判断表达式:
    • if [ $a -gt $b ]
    • if [ $a -lt 5 ]
    • if [ $b -eq 10 ]
    • -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意到处都是空格
  • 可以使用 && || 结合多个条件
    • if [ $a -gt 5 ] && [ $a -lt 10 ]; then 并且
    • if [ $b -gt 5 ] || [ $b -lt 3 ]; then 或者

二,DAY7

6.文件目录属性判断

  • [ -f file ]判断是否是普通文件,且存在

    #!/bin/bash
    f="/tmp/test.txt"
    if [ -f $f ]
    then
       echo $f exist
    else
       touch $f
    fi
    
    如果判断不存在,加上感叹号!
    #!/bin/bash
    f="/tmp/abc.txt"
    if [ ! -f $f ]
    then
       touch $f
    fi
  • [ -d file ] 判断是否是目录,且存在
  • [ -e file ] 判断文件或目录是否存在
  • [ -r file ] 判断文件是否可读
  • [ -w file ] 判断文件是否可写
  • [ -x file ] 判断文件是否可执行

7.if特殊用法

  • if [ -z "$a" ] 这个表示当变量a的值为空时会怎么样
    #!/bin/bash
    if [ ! -f /tmp/abc.log ]
    then
       echo "/tmp/abc.log not exist"
       exit
    fi
    n=`wc -l /tmp/abc.log`
    if [ -z "$n" ]
    then
       echo error
       exit
    elif [ $n -gt 100 ]
    then
       echo $n
    fi
  • if [ -n "$a" ] 表示当变量a的值不为空,可以判断一个文件的内容不为空
    [root@localhost tmp]# if [ -n if.sh ];then echo ok;fi  判断文件不用双引号
    ok
    [root@localhost tmp]# if [ -n "$a" ];then echo $a;else echo "a is null";fi    如果是变量一定要加双引号
    a is null
  • if grep -q '123' 1.txt; then表示如果1.txt中含有'123'的行时会怎么样
    [root@localhost tmp]# grep -w 'zabbix' /etc/passwd
    zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    [root@localhost tmp]# 
    [root@localhost tmp]# if grep -w 'zabbix' /etc/passwd;then echo "zabbix exist";fi
    zabbix:x:498:498:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    zabbix exist
    [root@localhost tmp]# if grep -wq 'zabbix' /etc/passwd;then echo "zabbix exist";fi    -q表示仅仅做过滤,不把过滤的内容显示出来
    zabbix exist
  • if [ ! -e file ]; then 表示文件不存在时会怎么样
  • if (($a<1)); then …等同于 if [ $a -lt 1 ]; then…
  • [ ] 中不能使用<,>,==,!=,>=,<=这样的符号

8.cace判断(上)

  • 格式:

    case 变量名 in
    value1)
    command
    ;;
    value2)
    command
    ;;  
    *)
    commond
    ;;
    esac
  • 在case程序中,可以在条件中使用|,表示或的意思, 比如
    - 2|3)
    command  
    ;;
  • 案例:

    #!/bin/bash
    read -p "Please input a number: " n
    if [ -z "$n" ]
    then
        echo "Please input a number."
        exit 1
    fi
    
    n1=`echo $n|sed 's/[0-9]//g'`
    if [ -n "$n1" ]
    then
     echo "Please input a number."
     exit 1
    fi
    
    if [ $n -lt 60 ] && [ $n -ge 0 ]
    then
        tag=1
    elif [ $n -ge 60 ] && [ $n -lt 80 ]
    then
        tag=2
    elif [ $n -ge 80 ]  && [ $n -lt 90 ]
    then
        tag=3
    elif [ $n -ge 90 ] && [ $n -le 100 ]
    then
        tag=4
    else
        tag=0
    fi
    case $tag in
        1)
            echo "not ok"
            ;;
        2)
            echo "ok"
            ;;
        3)
            echo "ook"
            ;;
        4)
            echo "oook"
            ;;
        *)
            echo "The number range is 0-100."
            ;;
    esac

    9.cace判断(下)

10.for循环

  • 语法:for 变量名 in 条件; do …; done
  • 案例1
    #!/bin/bash
    sum=0
    for i in `seq 1 100`
    do
      sum=$[$sum+$i]
      echo $i
    done
    echo $sum
  • 案例2
    
    文件列表循环
    #!/bin/bash
    cd /etc/
    for a in `ls /etc/`
    do
      if [ -d $a ]
      then
          ls -d $a
      fi
    done

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:3shell编程知识-创新互联
标题来源:http://cdkjz.cn/article/iggjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220