Linux shell数组建立及使用

Linux shell数组建立及使用_第1张图片

一:定义
二:数组的声明与定义
三:数组的读取与赋值
四:特殊使用
五:练习

一:定义

  • 变量:存储单个元素的内存空间

  • 数组:存储多个元素的连续的内存空间,相当于多个变量的集合

  • 数组名和索引

    • 索引:编号从0 开始,属于数值索引
      注意:索引可支持使用自定义的格式,而不仅是数值格式,即为关联索引, bash4.0 版本之后开始支持 bash 的数组支持稀疏格式(索引不连续)

二:数组的声明与定义

declare -a ARRAY_NAME
declare -A ARRAY_NAME: 关联数组
注意:两者不可相互转换

[root@centos7 ~]# a=(1 2 3 4 5)
[root@centos7 ~]# echo $a

一对括号表示的是数组,数组元素用空格符号分隔开

三:数组的读取与赋值

  • 得到长度
[root@centos7 ~]# echo ${#a[@]}5

用${#数组名[ @或* ]}可以得到数组长度

  • 读取
  [root@centos7 ~]# echo ${a[2]}3
 [root@centos7 ~]# echo ${a[*]}1 2 3 4 5

用${ 数组名[ 下标 ] },下标是从0开始 。下标是:*或者@得到整个数组内容

  • 赋值
[root@centos7 ~]# a[1]=100
[root@centos7 ~]# echo ${a[*]}
1 100 3 4 5
[root@centos7 ~]# a[5]=100
[root@centos7 ~]# echo ${a[*]}
1 100 3 4 5 100

直接通过 数组名[ 下标 ] 就可以对其直接进行引用赋值,如果下标不存在,自动添加一个新的数组元素

  • 删除
[root@centos7 ~]# a=(1 2 3 4 5)
[root@centos7 ~]# echo ${a[*]}
1 2 3 4 5
[root@centos7 ~]# unset a
[root@centos7 ~]# echo ${a[*]}
[root@centos7 ~]# a=(1 2 3 4 5)
[root@centos7 ~]# unset a[1]
[root@centos7 ~]# echo ${a[*]}
1 3 4 5
[root@centos7 ~]# echo ${#a[*]}4

直接通过:unset 数组[ 下标 ] 可以清除相应的元素,不带下标,清除整个数组

四:特殊使用

  • 分片
[root@centos7 ~]# a=(1 2 3 4 5)
[root@centos7 ~]# echo ${a[@]:0:3}
1 2 3
[root@centos7 ~]# echo ${a[@]:1:4}
2 3 4 5
[root@centos7 ~]# c=(${a[@]:1:4})
[root@centos7 ~]# echo ${#c[@]}
4
[root@centos7 ~]# echo ${c[*]}
2 3 4 5

直接通过 ${ 数组名 [ @或*]: 起始位置: 长度 } 切片原先的数组,返回时字符串,中间用空格分开,因此如果加上”( )”,将得到切片数组。上面的例子中,c 就是一个新的数组

  • 替换
[root@centos7 ~]# a=(1 2 3 4 5)
[root@centos7 ~]# echo ${a[@]/3/100}
1 2 100 4 5
[root@centos7 ~]# echo ${a[@]}
1 2 3 4 5
[root@centos7 ~]# a=(${a[@]/3/100})
[root@centos7 ~]# echo ${a[@]}
1 2 100 4 5

调用方法是:${ a[@或*] /查找字符/替换字符 },该操作不会改变原来的数组内容,如果需要修改,可以参考上面的例子,重新定义数组

从上面讲到的,大家可以发现Linux shell的数组已经很强大了。下面是一些实栈练习题

五:练习

1: 示例:生成10 个随机数保存于数组中,并找出其最大值和最小值

#!/bin/bash
declare -a rand
declare -i max=0
declare –i min=32767
for i in {0..9}; do
   rand[$i]=$RANDOM
   echo ${rand[$i]}
   [ ${rand[$i]} -gt $max ] && max=${rand[$i]}
   [ ${rand[$i]} -lt $min ] && min=${rand[$i]}
done
echo "Max: $max Min:$min"

2:编写脚本,定义一个数组,数组中的元素是/var/log 目录下所有以 .log 结尾的文件;要统计其下标为偶数的文件中的行数之和

#!/bin/bash
declare -a files
files=(/var/log/*.log)
declare -i lines=0
for i in $(seq 0 $[${#files[*]}-1]); do
    if [ $[$i%2] -eq 0 ];then
    let lines+=$(wc -l ${files[$i]} | cut -d' ' -f1)
    fi
done
echo "Lines: $lines."

3 、输入若干个数值存入数组中,采用冒泡算法进行升序或降序排序

[root@fengl bin]# cat sz.sh 
#!/bin/bash

declare -a arr
until echo ${arr[*]} | grep -E "^[[:digit:]‘ ‘]+$" &> /dev/null;
do
read -p "请输入一组数字,每个数字以空格隔开:" -a arr
done

up () {
local i=$[`echo ${#arr[*]}`-1]
while [ $i -ge 0 ] ;
do
    local n=$[i-1]
    while [ $n -ge 0 ] ;
    do
        if [[ ${arr[$i]} -lt ${arr[$n]} ]] ;then
            local x=${arr[$i]}
            local y=${arr[$n]}
            arr[$i]=$y
            arr[$n]=$x
        fi
        let n--
    done
    let i--
done
echo ${arr[*]}
}
down () {
local i=$[`echo ${#arr[*]}`-1]
while [ $i -ge 0 ] ;
do
        local n=$[i-1]
        while [ $n -ge 0 ] ;
        do
                if [[ ${arr[$i]} -gt ${arr[$n]} ]] ;then
                        local x=${arr[$i]}
                        local y=${arr[$n]}
                        arr[$i]=$y
                        arr[$n]=$x
                fi
                let n--
        done
        let i--
done
echo ${arr[*]}
}
echo "升序排列为:"
up
echo "降序排列为:"
down















你可能感兴趣的:(Linux shell数组建立及使用)