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

普通数组:只能使用整数 作为数组索引 【有序 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]}}'

  

 

shell 数组基础->的更多相关文章

  1. SHELL脚本--shell数组基础

    bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html 数组和变量的区别是:变量在内存中占用的空间是离散的,数组在内存 ...

  2. Shell基础(四):字符串截取及切割、字符串初值的处理、基使用Shell数组、expect预期交互、使用正则表达式

    一.字符串截取及切割 目标: 使用Shell完成各种Linux运维任务时,一旦涉及到判断.条件测试等相关操作时,往往需要对相关的命令输出进行过滤,提取出符合要求的字符串. 本案例要求熟悉字符串的常见处 ...

  3. 【转】Shell编程基础篇-上

    [转]Shell编程基础篇-上 1.1 前言 1.1.1 为什么学Shell Shell脚本语言是实现Linux/UNIX系统管理及自动化运维所必备的重要工具, Linux/UNIX系统的底层及基础应 ...

  4. centos shell脚本编程1 正则 shell脚本结构 read命令 date命令的用法 shell中的逻辑判断 if 判断文件、目录属性 shell数组简单用法 $( ) 和${ } 和$(( )) 与 sh -n sh -x sh -v 第三十五节课

    centos   shell脚本编程1 正则  shell脚本结构  read命令  date命令的用法  shell中的逻辑判断  if 判断文件.目录属性  shell数组简单用法 $( ) 和$ ...

  5. shell脚本-基础

    shell脚本-基础 编程基础 程序是指令+ 数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell 程序提供了编程能力,解释执行. 计算运行二进 ...

  6. 6-2 shell编程基础

    shell编程基础 编程基础 Linus:Talk is cheap, show me the code 程序和编程风格 程序: 程序:算法+数据结构 数据:是程序的核心 算法:处理数据的方式 数据结 ...

  7. 什么是Shell?Shell脚本基础知识详细介绍

    这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...

  8. Linux Shell 数组

    shell 数组一般都是一维数组. 1. 数组的声明 declare -a arr 该命令将声明一个数组arr,实际上不声明也可以直接定义数组. 2. 数组的初始化 arr=(1 2 3):该命令定义 ...

  9. Java 数组基础

    数组 数组(Array):相同类型数据的集合. 定义数组 方式1(推荐,更能表明数组类型) type[] 变量名 = new type[数组中元素的个数]; 比如: int[] a = new int ...

随机推荐

  1. maven简单理解

    前言: maven项目也是一个项目,类似于javaProject,javaWebProject,就是多了些功能,其他也没啥,所以大家接触的时候不要害怕! 1 . 帮你下载jar包 maven项目会有一 ...

  2. SAM

    后缀自动机能识别字符串S的所有子串,是一个DAG. http://blog.csdn.net/huanghongxun/article/details/51112764 https://blog.xe ...

  3. 【刷题】BZOJ 4657 tower

    Description Nick最近在玩一款很好玩的游戏,游戏规则是这样的: 有一个n*m的地图,地图上的每一个位置要么是空地,要么是炮塔,要么是一些BETA狗,Nick需要操纵炮塔攻击BETA狗们. ...

  4. ot

    https://blog.csdn.net/notice520/article/details/8135600 | android中的跨进程通信的实现(一)——远程调用过程和aidl - CSDN博客 ...

  5. BP神经网络人口预测程序(matlab实现)

    自己测试人口预测的matlab实现: x=[54167    55196    56300    57482    58796    60266    61465    62828    64653  ...

  6. HOJ 13101 The Triangle Division of the Convex Polygon(数论求卡特兰数(模不为素数))

    The Triangle Division of the Convex Polygon 题意:求 n 凸多边形可以有多少种方法分解成不相交的三角形,最后值模 m. 思路:卡特兰数的例子,只是模 m 让 ...

  7. 【Asp.net入门5-03】创建产品清单

  8. python 中的 %s,%r,__str__,__repr__

    1.%s,%r的区别 在进行格式化输出时,%r 与 %s 的区别就好比 repr() 函数处理对象与 str() 函数处理对象的差别. %s ⇒ str(),比较智能: %r ⇒ repr(),处理较 ...

  9. layoutSubviews中判断横竖屏

    在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏.具体代码: -(void)layoutSubviews{ [super layout ...

  10. mongodb3.6集群搭建:分片集群认证

    上篇集群已经创建,现在加入认证. 1. 生成密钥文件每个服务器上创建路径: mkdir -p /var/lib/mongo/auth 生成64字节的密钥文件openssl rand -base64 6 ...