Shell脚本(四)数组

平时写脚本还没有用到过数组,暂时先记录下用法。

#!/bin/bash

array1=(1 2 3 4 5)

array1_length=${#array1[@]}
echo "array1 length: ${array1_length}"

for item in ${array1[@]}
do
    echo "current item of array1: ${item}"
done

array2[0]="test0"
array2[1]="test1"
array2[2]="test2"
array2[3]="test3"

for((i=0; i < ${#array2[@]}; i++))
do
    echo "current_index: $i, current_value: ${array2[$i]}"
done

# 关联数组
declare -A dictArray
dictArray=([key3]=value3 [key1]=value1 [key2]=value2)

dictArray_length=${#dictArray[@]}
echo "dictArray length: ${dictArray_length}"

for key in ${!dictArray[@]}
do
    echo "current key of dictArray: ${key}"
done

for value in ${dictArray[@]}
do
    echo "current value of dictArray: ${value}"
done

for key in ${!dictArray[@]}
do
    echo "key: ${key}, value: ${dictArray[${key}]}"
done

 

posted @ 2017-07-28 17:51  Sawyer Ford  阅读(173)  评论(0编辑  收藏  举报