码迷,mamicode.com
首页 > 编程语言 > 详细

Shell脚本(五)数组

时间:2017-07-28 18:22:55      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:span   val   div   key   item   value   length   有用   style   

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

#!/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"

echo "array2: ${array2[@]}"

# 关联数组
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

 

Shell脚本(五)数组

标签:span   val   div   key   item   value   length   有用   style   

原文地址:http://www.cnblogs.com/gattaca/p/7251690.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!