• Shell(5)数组


    数组

    普通数组 只能以数字作为索引
    关联数组 可以使用字符串作为索引

    数组类似变量 不同的是变量只能有一个值 数组中可以放多个值
    变量:
       框子1=苹果
       框子2=香蕉
       框子3=茄子
       name=ahui
       ip=172.16.1.31
    数组:
       框子1[小盒子1]=苹果
       框子1[小盒子2]=香蕉
       框子1[小盒子3]=茄子

    数组语法:
    数组名称[索引的名称|下标]=值
    框子[盒子1]=苹果
    
    • 1
    • 2
    • 3

    第一种定义方式: 通过索引方式定义

    [root@test ]# array[0]=shell
    [root@test ]# array[1]=mysql
    [root@test ]# array[2]=docker
    [root@test ]# echo $array
    shell
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查看数组 按照索引的方式查看

    [root@test ]# echo ${array[1]}
    mysql
    [root@test ]# echo ${array[2]}
    docker
    [root@test ]# echo ${array[0]}
    shell
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    查看数组中所有的值

    [root@test ]# echo ${array[*]}
    shell mysql docke
    
    [root@test ]# echo ${array[@]}
    shell mysql docker
    
    • 1
    • 2
    • 3
    • 4
    • 5

    临时取消数组

    [root@test ]# unset array
    
    • 1

    第二种定义数组方式: 直接定义默认索引从0开始

    [root@test ]# array=(xiaohong xiaozi  xiaolv  xiaolan)
    [root@test ]# echo ${array[*]}
    xiaohong xiaozi xiaolv xiaolan
    [root@test ]# echo ${!array[*]}
    0 1 2 3
    
    • 1
    • 2
    • 3
    • 4
    • 5

    例:

    大保健: 红浪漫
    
    红浪漫[1]=xiaohong
    红浪漫[2]=xiaolv
    红浪漫[3]=xiaozi
    
    查看房间内容
    echo 红浪漫[1]
    xiaohong
    查看所有的
    echo 红浪漫[*]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    第三种定义方式: 直接定义和通过索引定义

    [root@test ]# array=(xiaozi  [666]=xiaolan [888]=xiaohong xiaolv )
    [root@test ]# echo ${array[*]}
    xiaozi xiaolan xiaohong xiaolv
    [root@test ]# echo ${!array[*]}
    0 666 888 889
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第四种定义方式: 命令定义

    [root@test ]# ls
    expect.ex  game.sh  jumpserver.sh  test.ex
    [root@test ]# array=(`ls`)
    [root@test ]# echo ${array[*]}
    expect.ex game.sh jumpserver.sh test.ex
    [root@test ]# echo ${!array[*]}
    0 1 2 3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    定义IP地址数组: 进行数组遍历

    [root@test ]# ip=(172.16.1.7 172.16.1.2 172.16.1.31 172.16.1.51)
    [root@test ]# echo ${ip[*]}
    172.16.1.7 172.16.1.2 172.16.1.31 172.16.1.51
    [root@test ]# 
    [root@test ]# for i in ${ip[*]};do echo $i;done
    172.16.1.7
    172.16.1.2
    172.16.1.31
    172.16.1.51
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    写入脚本

    [root@test ]# 
    [root@test ]# cat array.sh
    #!/bin/bash
    ip=(172.16.1.7 172.16.1.2 172.16.1.31 172.16.1.51)
    
    for i in ${ip[*]}
    do
    	  echo $i
    done
    [root@test ]# sh array.sh
    172.16.1.7
    172.16.1.2
    172.16.1.31
    172.16.1.51
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    通过索引来遍历数组

    [root@test ]# cat array.sh 
    #!/bin/bash
    ip=(172.16.1.7 172.16.1.2 172.16.1.31 172.16.1.51)
    
    for i in ${!ip[*]}
    do
    	  echo ${ip[$i]}
    done
    
    [root@test ]# sh array.sh
    172.16.1.7
    172.16.1.2
    172.16.1.31
    
    	172.16.1.51
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    案例: 取数组中的值

    [root@test ]# cat array.sh 
    #!/bin/bash
    ip=(172.16.1.7 172.16.1.2 172.16.1.31 172.16.1.51)
    for i in ${ip[*]}
    do
    	   ping -c1 -W1 $i &>/dev/null
    	   if [  $? -eq 0 ];then
    	   echo "ping $i is ok...."
    	   else
    	 echo "ping $i is error..."
    	   fi
    done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    查看普通数组:

    declare -a array='([0]="docker")'
    
    • 1

    关联数组: 定义关联数组需要声明

    [root@test ]# declare -A array
    [root@test ]# array[index1]=shell
    [root@test ]# array[index2]=mysql
    [root@test ]# array[index3]=docker
    [root@test ]# echo ${array[*]}
    shell mysql docker
    [root@test ]# echo ${!array[*]}
    index1 index2 index3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    案例:array[a]++ ========= array[a]=array[a+1]

    [root@test ]# unset array
    [root@test ]# declare -A array
    [root@test ]# #a b c 
    [root@test ]# let a++
    [root@test ]# let a++
    [root@test ]# let b++
    [root@test ]# let c++
    [root@test ]# echo $a $b $c
    2 1 1
    [root@test ]# let array[a]++
    [root@test ]# let array[a]++
    [root@test ]# let array[b]++
    [root@test ]# let array[c]++
    [root@test ]# echo ${array[a]}
    2
    [root@test ]# echo ${array[b]}
    1
    [root@test ]# echo ${array[c]}
    1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    案例:使用for循环依次取值:

    [root@test ]# cat user.txt 
    a
    a
    b
    c
    [root@test ]# #i=a
    [root@test ]# #let array[$i]++
    [root@test ]# #let array[a]++
    [root@test ]# #let array[a]++
    [root@test ]# #echo ${array[a]}
    [root@test ]# #2
    [root@test ]# #let array[b]++
    [root@test ]# #let array[c]++
    [root@test ]# echo ${array[a]}
    2
    [root@test ]# #echo ${array[a]}
    [root@test ]# #echo ${array[b]}
    [root@test ]# #echo ${array[c]}
    
    
    [root@test ]# cat array.sh
    #!/bin/bash
    declare -A array 
    for i in `cat user.txt`
    do
    	let array[$i]++
    done
    for i in  ${!array[*]}
    do
    	echo $i 出现了 ${array[$i]}done
    [root@test ]# sh array.sh
    a 出现了 2 次
    b 出现了 1 次
    c 出现了 1
    • 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
    • 32
    • 33
    • 34
    • 35

    案例:统计bash的数量

    [root@test ]# cat array1.sh
    #!/bin/sh
    declare -A array
    while read line
    do
    	 bash=`echo $line|awk -F: '{print $NF}'`
    	 let array[$bash]++
    done</etc/passwd
    
    for i in ${!array[*]}
    do
    	echo $i 出现了 ${array[$i]}done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试

    [root@test ]# sh array1.sh
    /sbin/nologin 出现了 17 次
    /bin/sync 出现了 1 次
    /bin/bash 出现了 26 次
    /sbin/shutdown 出现了 1 次
    /sbin/halt 出现了 1
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    案例:数组统计IP出现次数:

    [root@test ]# cat array1.sh 
    #!/bin/sh
    declare -A ips
    while read line
    do
    	 ip=`echo $line|awk '{print $1}'`
    	 let ips[$ip]++
    done</var/log/nginx/access.log
    
    for i in ${!ips[*]}
    do
    	echo $i 出现了 ${ips[$i]}done
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
  • 相关阅读:
    计算机毕业设计Java基层党支部建设平台(源码+系统+mysql数据库+lw文档)
    【软件工程】白盒测试:基本路径测试
    MySQL数据类型
    Flutter中系统Emoji通过substring裁切后无法识别导致渲染错误
    1336_FreeRTOS中一组队列辅助接口函数的实现分析
    MySQL -- 库和表的操作
    名牌大学毕业,在名企担任程序员月薪5万,却为何选择离职当司机
    智能文档处理黑科技,拥抱更高效的数字世界
    结合OD和《植物大战僵尸》,实现随意过关
    Windows 10 无法访问某文件夹无法访问(如C:\Documents and Settings)。拒绝访问。解决方法
  • 原文地址:https://blog.csdn.net/AHui_CSDN/article/details/126182863