shell数组基本概念

shell数组变量:

 普通数组:只能使用整数作为数组索引

 关联数组:可以使用字符串作为数组索引

数组变量与普通变量对比:

 普通变量:只能存储一个值

 数组变量:可以存储多个值

 例如:

   name=playgame  变量

  ----------------------------------------- 

  |p |l |a |y |g |a |m |e

  |0 |1 |2 |3 |4 |5 |6 |7  索引

  -----------------------------------------

变量的引用:

  echo $name    

    playgame 

  echo ${name:0:4}  切片操作

    play

   echo ${name:4}

    game

   echo  ${#name}    变量元素个数

    4  

   books=(history english mathematics chinese chemistry physics)      普通数组

   books=([0]=history  [1]=english  [2]=mathematics  [3]=chinese  [4]=chemistry  [5]=physics) (系统写法)

  -------------------------------------------------------------------------------------------

   |history |english |mathematics |chinese |chemistry |physics

   |0     |1      |2      |3    |4      |5      索引

  -------------------------------------------------------------------------------------------

 数组的引用: 

  echo ${books[@]}  全部数组元素

     history english mathematics chinese chemistry physics

  echo  ${!books[@]}  数组元素的索引(重要

    0  1  2  3  4  5

  echo ${books[0]}   单个数组元素

    history

   echo  ${#books[@]}  数组元素的个数

    6

  echo ${books[@]:0:2}  切片操作

    history english

     

info=([name]=wang)

info+=([sex]=woman [age]=22 [height]=172 [skill]=english)            关联数组的定义,等价于如下语法

  info=([name]=wang  [sex]=woman [age]=22  [height]=172  [skill]=english)  关联数组定义,同上。          

  -------------------------------------------------------------------------------------------

   |wang  |woman  |22    |172   |english

   |name  |sex     |age  |height  |skill        索引

  -------------------------------------------------------------------------------------------

 关联数组的引用:

  echo ${info[@]}      全部数组元素

    wang  woman  22  172  english  

  echo ${!info[@]}      数组元素的索引(重要

    name sex age height skill

  echo  ${info[age]}     单个数组元素

    22

   echo ${#info[@]}    数组元素的个数

    5

定义数组方式:

  方法一:一次赋一个值

    array1[0]=pear

    array1[1]=apple

    array1[2]=banana

    array1[3]=orange

  方法二:一次赋多个值

    array1=(pear  apple   banana   orange)       普通值作为数组元素

    array2=(1  2  3  4  "linux shell" [20]=ansile)       量值可以是字符串,可以跳过默认的变量索引直接定义值

    array3=(`cat /var/shell/a.txt`)           命令的值作为数组元素

    array4=($red  $green  $blue  $black  $white)    变量作为数组元素

  查看数组:

    decale -a    查看普通数组,系统默认

    decale -A     查看关联数组

     

 扩展:

  

[root@x112 script]# ls dir 
hbyd_2020101110_14.jpg hbyd_2020101121_17.jpg hbyd_2020101132_14.jpg hbyd_2020101143_17.jpg
hbyd_2020101110_17.jpg hbyd_202010112_14.jpg hbyd_2020101132_17.jpg hbyd_2020101144_14.jpg
[root@x112 script]# cat for1.sh 
#!/bin/bash
result=(`ls dir`)

for  i in ${!result[@]} 
do
	echo $i: ${result[i]}
done
[root@x112 script]# sh for1.sh 
0: hbyd_2020101110_14.jpg
1: hbyd_2020101110_17.jpg
2: hbyd_2020101111_14.jpg
3: hbyd_2020101111_17.jpg
4: hbyd_202010111_14.jpg
5: hbyd_202010111_17.jpg
6: hbyd_2020101112_14.jpg
7: hbyd_2020101112_17.jpg
8: hbyd_2020101113_14.jpg

  

  

原文地址:https://www.cnblogs.com/xiaofeng666/p/13767828.html