zoukankan      html  css  js  c++  java
  • shell 数组介绍

      平时的定义a=1;b=2;c=3,变量如果多了,再一个一个定义很繁琐,并且取变量值也很累。简单的说,数组就是各种数据类型的元素按一定顺序排列的集合。

             数组就是把有限个元素变量或数组用一个名字命名,然后用编号区分他们的变量的集合。这个名字成为数组名,编号成为数组下标。组成数组的各个变量成为数组的分量,也称为数组的元素,有时也称为下标变量。

             如果有过用其它语言编程的经历,那么想必会熟悉数组的概念。由于有了数组,可以用相同的名字引用一系列变量,并用数字(索引)来识别它们。在许多场合,使用数组可以缩短和简化程序开发,因为可以利用索引值设计一个循环,高效处理多种情况。

    数组定义与增删改查:

    数组定义:array=(1 2 3) 

    获取数组的长度:echo ${#array[@]}  或echo ${#array[*]}

    打印数组元素:echo ${array[0]} 或echo ${array[1]}

    打印数组所有元素:echo ${array[*]} 或echo ${array[@]}

    测试:用数组定义ip地址,然后用for循环打印出来

    vim array.sh
    array=(
    10.1.1.1
    10.1.1.2
    10.1.1.3
    )
    for ip in ${array[*]}
    do
        echo $ip
        sleep 1
    done
    C语言型:
    array=(
    10.1.1.1
    10.1.1.2
    10.1.1.3
    )
    for ((i=0;i<${array[@]};i++))
    do
    echo ${array[]i}
    done

    数组赋值:

    [root@bqh-118 scripts]# array=(1 2 3)
    [root@bqh-118 scripts]# echo ${array[*]}
    1 2 3
    [root@bqh-118 scripts]# array[3]=4    #增加数组元素
    [root@bqh-118 scripts]# echo ${array[*]}
    1 2 3 4
    [root@bqh-118 scripts]# array[0]=bqh     #修改数组元素
    [root@bqh-118 scripts]# echo ${array[*]}
    bqh 2 3 4

    数组删除:直接用unset数组[下标]可以清楚相应的元素,不带下标,清楚整个数组

    [root@bqh-118 scripts]# echo ${array[*]}
    bqh 2 3 4
    [root@bqh-118 scripts]# unset array[0]
    [root@bqh-118 scripts]# echo ${array[*]}
    2 3 4
    [root@bqh-118 scripts]# unset array
    [root@bqh-118 scripts]# echo ${array[*]}
     
    [root@bqh-118 scripts]#

    数组内容的截取和替换:

    截取:

    [root@bqh-118 scripts]# array=(1 2 3 4 5)
    [root@bqh-118 scripts]# echo ${array[*]:1:3}
    2 3 4
    [root@bqh-118 scripts]# echo ${array[*]:3:2}
    4 5

    调用方法:${数组名[@或*]:起始位置:长度}

    替换:

    [root@bqh-118 scripts]# echo ${array[*]/5/6}   #把数组中的5替换成6,临时生效,和sed很像
    1 2 3 4 6
    [root@bqh-118 scripts]# echo ${array[*]}
    1 2 3 4 5

    调用方法:${数组名[*或@]/查找字符/替换字符}

    -----------------------------------------------------------------------

    测试:把系统命令结果作为数组元素,然后一一打印出来

    [root@bqh-118 scripts]# array=($(ls))    #或array=($`ls`)
    [root@bqh-118 scripts]# echo ${array[0]}
    array.sh
    [root@bqh-118 scripts]# echo ${array[1]}
    bqh.sh
    [root@bqh-118 scripts]# echo ${array[*]}
    array.sh bqh.sh for.sh nginx nginx1 nginx.sh shoujicz.sh sjyjcx.sh while1_100sum.sh while_rz.sh while.sh while_sjcz.sh
    [root@bqh-118 scripts]#

    [root@bqh-118 scripts]# vim array1.sh 
    #!/bin/sh
    array=(ls cd pwd chmod charry echo mkdir awk sed grep) for i in ${array[*]} do echo $i done echo "===========================" array1=(ls cd pwd chmod charry echo mkdir awk sed grep)
    for ((i=0;i<${#array1[*]};i++)) do echo ${array1[i]} done

    OK,我们来一个实战测试:

    批量检查网站状态(数组for循环方法)

    首先拿一个一个网站检查,没问题了在用数组定义批量检测。

     我们常用curl、nmap、ping等方式检测,下为curl方法:

    [root@bqh-118 scripts]# curl -I www.baidu.com
    HTTP/1.1 200 OK
    Accept-Ranges: bytes
    Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
    Connection: Keep-Alive
    Content-Length: 277
    Content-Type: text/html
    Date: Wed, 15 May 2019 15:21:10 GMT
    Etag: "575e1f6f-115"
    Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
    Pragma: no-cache
    Server: bfe/1.0.8.18
    
    [root@bqh-118 scripts]# curl -I www.baidu.com 2>/devnull|egrep "200|302"
    HTTP/1.1 200 OK
    [root@bqh-118 scripts]# curl -I www.baidu.com 2>/devnull|egrep "200|302"|wc -l
    1
    [root@bqh-118 scripts]# curl -I www.taobao.com 2>/devnull|egrep "200|302"
    HTTP/1.1 302 Found
    [root@bqh-118 scripts]# curl -I www.taobao.com 2>/devnull|egrep "200|302"|wc -l
    1
    [root@bqh-118 scripts]# 

    我们只需要检测状态信息返回值有200或302时就代表网站畅通,反之异常。

    单个测试没问题,我们接着写脚本:

    [root@bqh-118 scripts]# vim curl.sh 
    #!/bin/sh
    # ******************************************************
    # Author       : aゞ锦衣卫 
    # Last modified: 2019-05-15 23:35
    # Email        : 1147076062@qq.com
    # blog         : https://www.cnblogs.com/su-root
    # Filename     : curl.sh
    # Description  :curl jiance
    # ******************************************************
    array=(
    www.baidu.com
    www.kanq.com.cn
    www.bqh.com
    www.jyw.org
    www.taobao.com
    www.jd.com
    )
    for n in ${array[*]}
    do
    curl=`curl -I -m 3 $n 2>/dev/null|egrep "200|302"|wc -l`
      if [ $curl -eq 1 ];then
        echo -e "$n 33[32m is ok!33[0m"
      else
        echo -e "$n 33[33;5m is not ok!33[0m"
      fi
    done

    当然还有其它很多方法,这里只介绍了curl。

    其它方法:

    ping ip地址/域名 #等于0

    nmap ip地址 -p 端口|grep open|wc -l #等于1

    wget --spider --timeout=10 --tries=2 ip地址 &>/dev/null #返回值等于0

  • 相关阅读:
    解决Oracle SQL Developer无法连接远程服务器的问题
    [备忘] Automatically reset Windows Update components
    在ASP.NET MVC的Action中直接接受客户端发送过来的HTML内容片段
    Rehosting the Workflow Designer
    解决Onedrive经常无法访问的问题
    最好的简明NodeJS学习材料
    最好的Python简明教程
    在Linux(ubuntu server)上面安装NodeJS的正确姿势
    在Windows中安装NodeJS的正确姿势
    在Windows环境中开始Docker的学习和体验
  • 原文地址:https://www.cnblogs.com/su-root/p/10872651.html
Copyright © 2011-2022 走看看