发表于 2018-8-30 06:47:09

Linux Shell编程之四数组

  shell编程之数组
  什么是数组呢?
  在编程语言中,数组是具有相同数据类型变量的集合,在内存中存储在一段连续的空间里。在 Linux shell 中数组也是这么定义,但用法和使用格式具有自己的特点。
  下面从不同角度来解析数组。
  一、数组的表示
  索引表示,一般使用数字做下标,从 0 开始:
  例如:a,a,a
  关联数组,数组的下标可以使用任意字符(符合变量命名的规则):
  例如:a,a
  在 shell 中只支持一维数组,支持稀疏格式。
  二、数组的定义
  declaer -a sum
  read -p "please input a array:"-a sum
  declare -A sum# 定义关联数组,如果使用关联数组必须先定义
  三、数组赋值
  一次只对一个赋值
  a=1,a=$RANDOM
  一次对全部元素赋值,例如:
  a=(red yellow black tigger mouse)
  按索引进行赋值
  a=(=less =more =cat =tail) # 稀疏格式存储
  四、数组访问
  用索引访问,例如:
  a a
  引用数组的值
  ${a},${a}
  全部引用:
  ${a[@]},${a
[*]} 一般情况下使用 ${a[@]} 这种形式,具体使用格式区别如下:
# cat test\@.sh  
#!/bin/bash
  
for ((i=0;i < 5;i++));do
  
    a[$i]=$RANDOM
  
done
  
for loop in "${a[@]}";do
  
    echo $loop
  
done
  
for loop in "${a
[*]}";do
  
    echo $loop
  
done
  

  
# ${a[@]} 这种形式列出所有的参数,每个参数是一个独立的字符串
  
# ${a
[*]} 也是列出所有的参数,单数所有的参数是一个字符串
  
# 执行结果如下:
  
# ./test\@.sh
  
32181
  
30658
  
26576
  
16071
  
6521
  
32181 30658 26576 16071 6521
  五、数组的长度
  ${#a
[*]},${#a[@]} 都可以表示数组的长度,在遍历数组时会使用到。
# a=(1 2 more less tail head)  
# echo ${#a[@]}
  
6
  
# echo ${#a
[*]}
  
6
  6、从数组中挑选某元素:
  ${ARRAY[@]:offset:number}
  offset: 偏移的元素个数
  number: 取出的元素的个数
# a=(1 2 more less tail head)  
# echo ${a[@]:1:2}
  
2 more
  ${ARRAY[@]:offset}:取出偏移量后的所有元素
# echo ${a[@]:2}  
more less tail head
  ${ARRAY[@]}: 取出所有元素
# echo ${a[@]}  
1 2 more less tail head
  七、向数组追加元素和从数组中删除元素
  这里一般针对的是非关联数组,也就是使用数字作为下标的数组
  追加元素:
# 新追加的元素的下标是数组的长度  
# a=(1 2 more less tail head)
  
# a[${#a[@]}]=33
  
# echo ${a[@]}
  
1 2 more less tail head 33
  删除数组中的元素,使用unset ARRAY
# unset a  
# echo ${a[@]}
  
1 2 less tail head 33
  八、数组的一些应用
  1、复制一个数组中下标为偶数的元素至一个新数组中
#!/bin/bash  
declare -a mylogs
  
logs=(/var/log/*.log)
  

  
echo ${logs[@]}
  

  
for i in `seq 0 ${#logs[@]}`; do
  
    if [ $[$i%2] -eq 0 ];then
  
      index=${#mylogs[@]}
  
      mylogs[$index]=${logs[$i]}
  
    fi
  
done
  

  
echo ${mylogs[@]}
  

  
# 这里最关键的是这一句 index=${#mylogs[@]}
  
# 实现了新数组的下标的设定
  2、统计 /etc/passwd 中使用各个shell的次数
#!/bin/bash  

  
declare -A sum
  

  
while read line ;do
  
    index=`echo $line | cut -d: -f7`
  
    let sum[$index]++
  
done < /etc/passwd
  

  
index_set=`cut -d: -f7 /etc/passwd | sort | uniq`
  

  
for loop in $index_set;do
  
    echo "$loop in /etc/passwd total have ${sum[$loop]} times."
  
done
  

  
# 这里主要是考察关联数组的应用。真正在实际中这种用法较多。
  执行结果如下:
# ./sumbash.sh  
/bin/bash in /etc/passwd total have 56 times.
  
/bin/sync in /etc/passwd total have 1 times.
  
/bin/tcsh in /etc/passwd total have 1 times.
  
/sbin/halt in /etc/passwd total have 1 times.
  
/sbin/nologin in /etc/passwd total have 33 times.
  
/sbin/shutdown in /etc/passwd total have 1 times.
  2、将 10 个随机数按升序排列
#!/bin/bash  

  
for i in {0..9};do
  
    arr[$i]=$RANDOM
  
done
  

  
echo "${arr[@]}"
  

  
let -i tmp
  

  
for i in `seq 0 $[${#arr[@]}-1]`;do
  
    for j in `seq $[$i+1] $[${#arr[@]}-1]` ;do
  
      if [${arr[$j]} -lt ${arr[$i]} ];then
  
            tmp=${arr[$j]}
  
            arr[$j]=${arr[$i]}
  
            arr[$i]=$tmp
  
      fi
  
    done
  
done
  

  
echo "${arr[@]}"
  

  
# 这个示例只在说明数组的使用形式,shell 中的数组一般不进行这样的工作。
  执行结果:
# ./sort.sh  
749 18853 896 20690 4794 395 23231 24049 4906 21630
  
395 749 896 4794 4906 18853 20690 21630 23231 24049
  至此,数组的基本使用就介绍完毕。


页: [1]
查看完整版本: Linux Shell编程之四数组