美文网首页
shell-数组

shell-数组

作者: linux_豪哥 | 来源:发表于2020-12-20 13:48 被阅读0次

1. 数组

1.1 什么是shell数组

shell的数组就是把有限个元素(变量或字符内容)用一个名字命名,然后用编号对他们进行区分的元素集合。这个名字就称为数组名,用于区分不同内容的编号就称为数组下标。组成数组的各个元素(变量)称为数组的元素,有时也称为下标变量。下标默认是从0开始。

1.2 数组的分类

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

1.3 数组的赋值方式

数组赋值方式: 一次赋多个值
数组名=(多个变量值)

[root@shell scripts]# array=(1 2 3 4 5)
[root@shell scripts]# echo ${array[0]}
1
[root@shell scripts]# echo ${array[1]}
2
[root@shell scripts]# echo ${array[2]}
3
[root@shell scripts]# echo ${array[3]}
4
[root@shell scripts]# echo ${array[4]}
5

数组赋值方式:针对每个索引进行赋值
数组名[索引]=变量值

[root@shell ~]# arraytest[0]=beijing
[root@shell ~]# arraytest[1]=shenzhen
[root@shell ~]# arraytest[2]=shanghai

数组赋值方式:第三种

[root@shell scripts]#array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# echo ${array[*]}
one two three
[root@shell scripts]# echo ${array[0]}

[root@shell scripts]# echo ${array[1]}
one 

数组赋值方式:动态赋值

array=($(ls))
array=(`ls`)

覆盖

[root@shell scripts]# array=([1]=one [2]=two [3]=three) 
[root@shell scripts]# echo ${array[*]}
one two three
[root@shell scripts]# echo ${array[0]}

[root@shell scripts]# echo ${array[1]}
one
[root@oshell scripts]# array[0]=a;array[1]=b;array[2]=c
[root@shell scripts]# echo ${array[*]}
a b c three

1.4 查看数组

查看数组所有元素
[root@shellscripts]# echo ${array[*]}
1 2 3 4 5
[root@shell scripts]# echo ${array[@]}
1 2 3 4 5
统计元素的个数
[root@shell scripts]# echo ${#array[@]} 
5
[root@shell scripts]# echo ${#array[*]}
5
获取数组元素的索引 
[root@shell scripts]# echo ${!array[@]}
0 1 2 3 10

1.5 查看数组的赋值结果

[root@shell ~]#  declare –a     查看普通数组
[root@shell ~]#  declare –A     查看关联数组

1.6 关联数组

关联数组可以用字符串当索引

1.6.1定义关联数组
[root@shell ~]# declare -A array_1
[root@shell ~]# declare -A array_2
1.6.2 关联数组赋值

关联数组的赋值方式一,针对每个索引进行赋值

#数组名[索引]=变量值
[root@shell ~]# declare -A array_1
[root@shell ~]# declare -A array_2
[root@shell ~]# array_1[index1]=beijing
[root@shell ~]# array_1[index2]=shenzhen
[root@shell ~]# array_1[index3]=shanghai

关联数组的赋值方式二,一次赋多个值

[root@shell ~]#  array_2=([index1]=beijing [index2]=shenzhen [index3]=shanghai)

访问数据元素

[root@shell ~]# echo ${array_2[index2]}
shenzhen
[root@shell ~]# echo ${array_2[@]}     
beijing shenzhen shanghai
[root@shell ~]# echo ${!array_2[@]} 
index1 index2 index3

1.7 例题
输出123456两种方法

array=(1 2 3 4 5)
for n in ${array[*]}
do
    echo $n
done

echo --------------
for((i=0;i<${#array[*]};i++))
do
    echo ${array[i]}
done  

利用bash for循环打印下面这句话中字母数不大于6的单词(某企业面试真题)。
I am English teacher welcome to English training class
要求用定义数组完成

array=(I am English teacher welcome to English training c
lass)
echo "第一种"
for n in ${array[*]}
do
    if [ ${#n} -lt 7 ]
    then
        echo $n
    else
        continue
    fi
done

echo "第二种"
for((i=0;i<${#array[*]};i++))
do
   [ ${#array[i]} -le 6 ]&&
       echo ${array[i]}
done
方法3:通过awk循环实现。
[root@shell scripts]# chars="I am English teacher welcome to English training class"
[root@shell scripts]# echo $chars|awk '{for(i=1;i<=NF;i++) if(length($i)<=6)print $i}' 

1.8 遍历数组

范例1

已知文件里有名字对应的性别 请统计性别出现的总次数
let array_pa[m]++
let array_pa[f]++
let array_pa[x]++
let array_pa[m]++
其实就是对相同索引进行计数

[root@shell ~]# cat sex.txt 
gs m
eg m
mz f
wr m
zs f
ls m
[root@shell ~]# cat count_sex.sh 
#!/bin/sh
declare -A sex
while read line
do
        type=$(echo $line|awk '{print $2}')
        let sex[$type]++
done<sex.txt
#遍历数组
for i in ${!sex[*]}
do
        echo $i 共有${sex[$i]}个
done

范例2

统计nginx日志IP访问次数
[root@shell ~]# cat nginx_count.sh 
#!/bin/sh
declare -A array_nginx
while read line
do
        type=$(echo $line|awk '{print $1}')
        let array_nginx[$type]++

done</var/log/nginx/access.log

for i in ${!array_nginx[*]}
do
      
    echo "IP是 $i 共出现了${array_nginx[$i]}次"
done

案例3

统计tcp状态信息
[root@shell ~]# cat nginx_status.sh 
#!/bin/sh
declare -A array_nginx
type=`ss -an|grep 80|awk '{print $2}'`
for i in $type
do
        let     array_nginx[$i]++
done
for n in ${!array_nginx[*]}
do
        echo $n ${array_nginx[$n]}
done

相关文章

  • shell-数组

    Shell在编程方面比Windows批处理强大很多,无论是在循环、运算。 bash支持一维数组(不支持多维数组),...

  • shell-数组

    数组 语法 : 数组名=(值1 值2 值3) 初始化2-1: name=("jianshu1" “jianshu2...

  • shell-数组

    1. 数组 1.1 什么是shell数组 1.2 数组的分类 1.3 数组的赋值方式 数组赋值方式: 一次赋多个值...

  • shell-函数、数组

    函数格式 示例

  • Jenkins脚本上传文件到SVN

    直接上例子1.构建->增加构建步骤->执行shell-复制粘贴->根据自身的需求做相关更改即可

  • shell-命令

    echo echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式:echo arg您可以使用...

  • shell-变量

    变量类型 运行shell时,会同时存在三种变量: 局部变量局部变量在脚本或命令中定义,仅在当前shell实例中有效...

  • shell-函数

    函数 函数可以让我们将一个复杂功能划分成若干模块,让程序结构更加清晰,代码重复利用率更高。像其他编程语言一样,Sh...

  • shell-判断

    文件测试-操作符:-gt 大于 | -lt 小于 | -eq 等于 | -ne 不等于 | ...

  • shell-通配符

    实例

网友评论

      本文标题:shell-数组

      本文链接:https://www.haomeiwen.com/subject/vefknktx.html