微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

shell 编程数组总结

数组总结

目录:

  1. 数组组成

  2. 数组赋值

  3. 数组输出

  4. 数组案例

1.数组组成

数组的组成就是一个元素的集合,将多个元素利用一个变量存储,避免一个元素采用一个变量而导致形成大量的变量,数组构成由数组名(变量)、元素(变量值)和数组下标组,读取一个数组时采用语法结构为:${变量名[索引编号]},其等价于{$变量名[1]、$变量名[2]..$变量名[n]}。

数组的分类主要分为两类,第一类是普通数组,普通数组索引编号是连续的,申明普通数组采用语法结构:delcare �Ca 数组名。第二类是关联数组,关联数组是索引编号不连续的的数组,申明关联数组采用语法结构:declare �CA 数组名。

2.数组赋值

变量的复制类似for循环中循环取值,可以通过多种方式进行赋值,具体方法如下:

方法1:通过小括号将整数列表直接值赋值给数组

#!/bin/bash

declare -a arr

arr=( 1 2 3 ) #将定义整数列表赋值

for i in {0..2};do

echo "this is the $i times"

echo "value=${arr[$i]}"

done

方法:2:通过命令返回值进行

[root@centos7 ~/txt]#vim array.sh

#!/bin/bash

arr=( $(ls /root/txt/) )

for i in {0..2};do

echo "this is the $i times"

echo "value=${arr[$i]}"

done

[root@centos7 ~/txt]#./array.sh #执行脚本则显示脚本名称,仅仅是脚本名称而已

this is the 0 times

value=1.txt

this is the 1 times

value=2.txt

this is the 2 times

value=3.txt

方法3:通过通配符进行赋值

[root@centos7 ~/txt]#vim array.sh

#!/bin/bash

declare -a arr1

arr1=(/root/txt/*.txt) #数组赋值文件,对文件进行处理

for j in {0..2};do

echo "this is the $j times"

[ -f ${arr1[$j]} ] && echo zunzai || echo bucunzai #通过数组元素判断文件是否存在

lines=`cat ${arr1[$j]} |wc -l` #统计每个文件的行号

let sum+=$lines

echo $sum

echo "value=${arr1[$j]}" #通过数组显示文件名称

done

[root@centos7 ~/txt]#./array.sh #验证执行结果

this is the 0 times

this is first txt.word

value=1.txt

this is the 1 times

this is sencond txt.word

value=2.txt

this is the 2 times

this is three txt.word

value=3.txt

3.数组输出

数组相当于一系列的变量元素的集合,在数组输出时,可以输出指定的元素、输出整体的元素和元素的个数:

1.输出整体的元素,采用语法结构为${变量名[*|@]},其中*|@表示通配符任意的意思,因此会输出所有的元素。

[root@centos7 ~/txt]#intarr=( 1 2 3 )

[root@centos7 ~/txt]#echo ${intarr[*]}

1 2 3

2.输出指定的元素,采用语法结构为${变量名[索引编号]},其中索引编号从0开始

[root@centos7 ~/txt]#intarr=( 1 2 3 )

[root@centos7 ~/txt]#echo ${intarr[0]}

1

[root@centos7 ~/txt]#echo ${intarr[1]}

2

3.在数组中修改其中某一个数组变量的元素或增加一个元素,采用语法结构为:变量名[索引编号]=***,当变量的索引编号存在时,覆盖变量元素原有的值,如若不存在变量的索引编号时,在数组中添加新增的变量元素。

[root@centos7 ~/txt]#intarr[1]=20 #存在索引编号1,因此覆盖原有值

[root@centos7 ~/txt]#echo ${intarr[1]}

20

[root@centos7 ~/txt]#intarr[3]=40 #不存在索引编号3,因此新增变量值

[root@centos7 ~/txt]#echo ${intarr[*]}

1 20 3 40

4.在数组中将某个元素删除采用语法结构为:unset 数组[索引编号],删除整个数组时:unset数组。

[root@centos7 ~/txt]#echo ${intarr[*]} #打印真个数组

1 20 3 40

[root@centos7 ~/txt]#unset intarr[3] #删除数组中第4个元素

[root@centos7 ~/txt]#echo ${intarr[*]}

1 20 3

[root@centos7 ~/txt]#unset intarr #删除真个数组

[root@centos7 ~/txt]#echo ${intarr[*]}

5.在数组中将截取某一段数组元素,语法结构分别为:${数组名[@]:offset:#},offset表示数组偏移个数,#表示取偏移后的多少个元素。

[root@centos7 ~/txt]#intarr=({a..z}) #生成序列

[root@centos7 ~/txt]#echo ${intarr[*]}

a b c d e f g h i j k l m n o p q r s t u v w x y z

[root@centos7 ~/txt]#echo ${intarr[@]:2:2} #取第2个元素后面的两个元素

c d

6.在数组中将数组某元素替换,语法结构分别为:${数组名[@]/###/***},offset表示数组中元素为###的替换为***,注意替换的是元素。

[root@centos7 ~/txt]#echo ${intarr[*]}

a b c d e f g h i j k l m n o p q r s t u v w x y z

[root@centos7 ~/txt]#echo ${intarr[@]/a/b} #匹配数组元素中的a进行替换为b

b b c d e f g h i j k l m n o p q r s t u v w x y z

4.数组案例

1.循环打印数组元素,数组包含IP地址:192.168.1.110.1.1.1 172.16.0.1;

#!/bin/bash

declare -a ip_arr

declare -i i=0

declare -i j

ip_arr=( #定义数组,数组元素为IP地址,充分说明数组就是变量的集合

192.168.1.1

10.1.1.1

172.16.0.1

)

###############################################

echo “方法1循环打印数组元素

for i in $(seq 0 $[${#ip_arr[*]}-1]);do

echo ${ip_arr[$i]}

let i++

done

###############################################

echo "方法2循环打印数组元素"

for (( j=0;j<${#ip_arr[*]};j++))

do

echo ${ip_arr[$j]}

done

###################将数组作为for循环中的元素进行循环##########################

echo "方法3数组遍历打印数组元素"

for n in ${ip_arr[*]};do

echo $n

done

2.统计文字或语句中的字符个数,思路:expr统计、wc统计、数组${#数组名[*]}和awk统计

#!/bin/bash echo -e "统计:\033[1;32mwelcome to sugon cloud IDC \033[0m语句中每个单词的字符"
declare -a arr
declare -i sum0=0
declare -i sum1=0
declare -i sum2=0
declare -i sum3=0
declare word1
declare word2
declare word3
arr=(`echo "welcome to sugon cloud IDC"`)
#+++++++++++++++++++++++数组直接统计元素个数++++++++++
echo "方法1:采用数组完成统计字符"
for i in $(seq 0 $[${#arr[*]}-1]);do #for循环的元素是整型数字
echo "The $i word have ${#arr[$i]} characters "
echo
let sum0+=${#arr[$i]}
done
echo The total characters:$sum0
#++++++++++++++++++++++++数组做for循环元素统计++++++++
echo "方法2:数组做元素完成统计" #for循环的元素是数组本身
for word1 in ${arr[*]};do
echo "The word have $(expr length $word1) characters"
echo
let sum1+=$(expr length $word1)
done
echo "The total characters:$sum1"
#+++++++++++++++++++++++++传统wc统计法+++++++++++++++++
echo "方法3:传统grep完成统计" #for循环的元素是数组本身

for word2 in ${arr[*]};do
echo "the word have $(echo $word2|grep -o . |wc -l) characters"
echo
let sum2+=$(echo $word2|grep -o . |wc -l)
done
echo "The total characters:$sum2"

#++++++++++++++++++++++++++wak统计法+++++++++++++++++++
echo "方法4:采用数组完成利用wak完成统计" #for循环的元素是数组本身

for word3 in ${arr[*]};do echo "the word have $(echo $word3|awk '{print length($0)}')" echo let sum3+=$(echo $word3|awk '{print length($0)}')doneecho "The total characters:$sum3"echo

原文地址:https://www.jb51.cc/bash/391024.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐