zoukankan      html  css  js  c++  java
  • shell 数组基础->

    数组其实也算是变量, 传统的变量只能存储一个值, 但数组可以存储多个值。

    普通数组:只能使用整数 作为数组索引 【有序 0 1 2 3 4 】
    关联数组:可以使用字符串 作为数组索引 【无序 name age tt】

    数组定义  

    普通数组定义 books=(nginx mysql php)
    普通数组调用


    echo ${books[0]}    # 调用数组中的第一个值
    echo ${books[@]}    # 调用所有的值
    echo ${!books[@]}    # 调用数组的所有索引

    关联数组定义
    declare -A books      # 申明这是一个关联数组
    books[name]=bgx    # 定义单个
    books=([name]=bgx [age]=18)   #定义多个数组
    let books[m]++      # 对中括号中的m索引进行自增运算

    遍历数组
    将需要统计的内容,作为数组的索引,使其自增,然后使用循环在此遍历出。

    普通数组赋值【普通数组】

    [root@web03 opt]# book=(nginx linux mysql)
    取值
    [root@web03 opt]# echo ${book[*]}
    nginx linux mysql
    [root@web03 opt]# echo ${book[0]}
    nginx
    [root@web03 opt]# echo ${book[1]}
    linux
    [root@web03 opt]# echo ${book[2]}
    mysql
    

    关联数组的赋值方式【关联数组】

    [root@web03 opt]# declare -A info
    [root@web03 opt]# info=([name]=bgx [age]=18 [sex]=m)
    [root@web03 opt]# echo ${info[age]}
    18
    [root@web03 opt]# echo ${info[name]}
    bgx
    [root@web03 opt]# echo ${info[sex]}
    m
    

    shell数组练习

    [root@web03 ~]# cat array_01.sh 
    #!/usr/bin/bash
    
    #1.先声明关联数组
    declare -A test_array
    
    #2.读入文件
    while read line
    do
    	#3.切割我们需要的内容[m f x ]
    	tt=$(echo $line|awk '{print $1}')
    	#4.进行赋值
    	let test_array[$tt]++
    done <zhuzhu.txt
    
    #3.取值
    
    for i in ${!test_array[@]}
    do
    	echo ${test_array[$i]} ${i}
    done
    

    统计/etc/passwd的shell数量

    [root@jumpserver-70 monday]# cat passwd_array.sh 
    #!/bin/bash
    
    declare -A array_passwd
    
    while read line
    do
    	type=$(echo $line | awk -F":" '{print $NF}')
    	let array_passwd[$type]++
    
    done</etc/passwd
    
    for i in ${!array_passwd[@]}
    do
    	echo ${array_passwd[$i]} ${i}
    done
    

    统计/etc/hosts

    [root@web03 ~]# cat array_03.sh 
    #!/usr/bin/bash
    #1.使用while读入一个文件
    while read line
    do
        #2.定义普通数组, 将读入的每行数据,单个单个进行赋值
        hosts[++i]=$line
    done </etc/hosts
    
    #3.使用for循环遍历数组, 遍历数组的索引
    for i in ${!hosts[@]}
    do
        echo "hosts数组对应的索引是:$i, 对应的值是: ${hosts[i]}"
    done
    

      

    统计nginx的ip访问次数

    [root@jumpserver-70 monday]# cat host_array.sh 
    #!/bin/bash
    
    declare -A array_nginx
    
    while read line
    do 
    	tt=$(echo $line |awk '{print $1}')
    	let array_nginx[$tt]++
    done<test.txt
    
    for i in ${!array_nginx[@]}
    do
    	echo "${array_nginx[$i]} $i"
    done
    

      

    统计tcp11种状态

    [root@jumpserver-70 monday]# cat status_array.sh 
    #!/bin/bash
    
    declare -A array_status
    
    for i in $(ss -an | awk '{print $2}')
    do
    	let array_status[$i]++
    done
    
    for n in ${!array_status[@]}
    do 
    	echo ${array_status[$n]} $n
    done

    awk取tcp的11种状态

    [root@web03 ~]# ss -an |awk '{ss_array[$2]++} END {for(i in ss_array) {print i,ss_array[i]}}'
    

      

     
  • 相关阅读:
    XML(学习笔记)
    css样式学习笔记
    Request(对象)
    sql一些错误修改的总结
    转载(如何学习C#)
    sql server(学习笔记2 W3Cschool)
    sql sqrver(学习笔记1 W3Cschool)
    关于 flutter开发碰到的各种问题,有的已经解决有的一直没解决或者用其他方法替代
    关于 Flutter IOS build It appears that your application still contains the default signing identifier.
    关于 flutter本地化问题 The getter 'pasteButtonLabel' was called on null
  • 原文地址:https://www.cnblogs.com/tim1blog/p/9703534.html
Copyright © 2011-2022 走看看