如果想出发,就不要等到明天!
全部博文(317)
分类: LINUX
2013-04-17 12:05:42
1) 常规数组的操作;
2)关联数组的操作;
Linux shell 的常规数组使用整数值作为索引值;
1.1 常规数组的声明:
1.1.1 使用一个元素列表声明并初始化一个常规数组
- array_var0=(a0 a1 a2 a3 a4 a5);
1.1.2 使用数组下标与元素值声明并初始化一个常规数组
- array_var1[1]="test1";
- array_var1[2]="test2";
- array_var1[3]="test3---";
- #array_var1[4]="test4";
- array_var1[5]="test5";
- array_var1[6]="test6";
- array_var1[7]="test7";
1.2 常规数组的访问:
1.2.1 常规数组元素的访问
使用数组名与索引值进行访问;
- ${arrayName[index]}
1.2.2 遍历常规数组的所有元素
- ${arrayName[*]}
- ${arrayName[@]}
1.2.3 获取常规数组的元素个数
- ${#arrayName[*]}
- ${#arrayName[@]}
1.2.4 获取常规数组所有元素的访问索引值
- ${!arrayName[*]}
- ${!arrayName[@]}
1.3 常规数组的访问操作实例:
- index=5;
- echo -e "\e[42;31m--- Manipulate an ordinary array which define with a list of values in a line! ---\e[0m";
- array_var0=(a0 a1 a2 a3 a4 a5);
- echo -e "\e[1;32m\"\${array_var0[0]}\"=\e[1;33m${array_var0[0]} \e[1;32m\"\${array_var0[$index]}\"=\e[1;33m${array_var0[$index]}\e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array array_var0 with \"\${array_var0[*]}\" are: \e[1;33m ${array_var0[*]} \e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array array_var0 with \"\${array_var0[@]}\" are: \e[1;33m ${array_var0[@]} \e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0 with \"\${array_var0[*]}\" is: \e[1;33m${#array_var0[*]}\e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0 with \"\${array_var0[@]}\" is: \e[1;33m${#array_var0[@]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array array_var0 with \"\${!array_var0[*]} are:\n \e[1;33m${!array_var0[*]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array array_var0 with \"\${!array_var0[@]} are:\n \e[1;33m${!array_var0[@]}\e[0m";
- echo -e "\e[42;31m--- Manipulate an ordinary array which define as a set of index-value pairs! ---\e[0m";
- array_var1[1]="test1";
- array_var1[2]="test2";
- array_var1[3]="test3---";
- #array_var1[4]="test4";
- array_var1[5]="test5";
- array_var1[6]="test6";
- array_var1[7]="test7";
- echo -e "\e[1;32m\"\${array_var1[0]}\"=\e[1;33m${array_var1[0]} \e[1;32m\"\${array_var1[$index]}\"=\e[1;33m${array_var1[$index]}\e[0m";
- echo -e "\e[1;32m\"\${array_var1[1]}\"=\e[1;33m${array_var1[1]} \e[1;32m\"\${array_var1[4]}\"=\e[1;33m${array_var1[4]}\e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array array_var1 with \"\${array_var1[*]}\" are:\n \e[1;33m ${array_var1[*]} \e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array array_var1 with \"\${array_var1[@]}\" are:\n \e[1;33m ${array_var1[@]} \e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0 with \"\${array_var1[*]}\" is: \e[1;33m${#array_var1[*]}\e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0 with \"\${array_var1[@]}\" is: \e[1;33m${#array_var1[@]}\e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0[3] with \"\${array_var1[3]}\" is: \e[1;33m${#array_var1[3]}\e[0m";
- echo -e "\e[1;32mObtain Length of array array_var0[4] with \"\${array_var1[4]}\" is: \e[1;33m${#array_var1[4]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array array_var0 with \"\${!array_var1[*]} are:\n \e[1;33m${!array_var1[*]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array array_var0 with \"\${!array_var1[@]} are:\n \e[1;33m${!array_var1[@]}\e[0m";
这里值得一提的是,Linux Shell 的常规数组没有在声明的时候规定好数组的长度,通过上面实验结果,我们也看到,数组的索引值列表里面并不包含 4 ,数组长度为 6 ;
由此可见 Linux Shell 常规数组里面的数组长度及元素空间是根据元素添加动态变化与分配空间的;
2. Linux Shell 关联数组
所谓关联数组就是使用字符串作为索引值对数组进行访问的数组;
2.1 关联数组的声明:
2.2 关联数组初始化与元素添加:
- declare -A arrayName;
2.2.1 使用 "[index]=value" 列表的方式对关联数组添加元素:
- declare -A assArray1;
- assArray1=([index1]=cat [index2]=dogs [index98]=fish);
2.2.2 使用 "arrayName[index]=value" 的方式对关联数组进行添加元素:
- declare -A assArray2;
- assArray2[pear]=100;
- assArray2[apple]=3000;
- assArray2[orange]=70000;
2.3 关联数组的访问:
2.3.1 关联数组的元素访问
关联数组使用 "arrayName[index]" 的方式访问数组元素
- ${arrayName[index]}
2.3.2 遍历关联数组的所有元素
- ${arrayName[*]}
- ${arrayName[@]}
2.3.3 获取关联数组的元素个数
- ${#arrayName[*]}
- ${#arrayName[@]}
2.3.4 获取关联数组所有可访问元素的索引值
- ${!arrayName[*]}
- ${!arrayName[@]}
2.4 关联数组的访问操作实例:
- echo -e "\e[42;31m--- Manipulate an associative array! ---\e[0m";
- echo -e "\e[1;32m --- Add elements by inline index-value pairs: ---\e[0m";
- index=index98;
- declare -A assArray1;
- assArray1=([index1]=cat [index2]=dogs [index98]=fish);
- echo -e "\e[1;32m\"\${assArray1[index1]}\"=\e[1;33m${assArray1[index1]} \e[1;32m\"\${assArray1[$index]}\"=\e[1;33m${assArray1[$index]}\e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array assArray1 with \"\${array_var1[*]}\" are:\n \e[1;33m ${assArray1[*]} \e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array assArray1 with \"\${array_var1[@]}\" are:\n \e[1;33m ${assArray1[@]} \e[0m";
- echo -e "\e[1;32mObtain Length of array assArray1 with \"\${assArray1[*]}\" is: \e[1;33m${#assArray1[*]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray1 with \"\${assArray1[@]}\" is: \e[1;33m${#assArray1[@]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray1[index2] with \"\${assArray1[index2]}\" is: \e[1;33m${#assArray1[index2]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray1[index98] with \"\${assArray1[\$index]}\" is: \e[1;33m${#assArray1[$index]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array assArray1 with \"\${!assArray1[*]} are:\n \e[1;33m${!assArray1[*]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array assArray1 with \"\${!assArray1[@]} are:\n \e[1;33m${!assArray1[@]}\e[0m";
- echo -e "\e[1;32m --- Add elements by separate index-value assignments: ---\e[0m";
- index=apple;
- declare -A assArray2;
- assArray2[pear]=100;
- assArray2[apple]=3000;
- assArray2[orange]=70000;
- echo -e "\e[1;32m\"\${assArray2[pear]}\"=\e[1;33m${assArray2[pear]} \e[1;32m\"\${assArray2[$index]}\"=\e[1;33m${assArray2[$index]}\e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array assArray2 with \"\${array_var1[*]}\" are:\n \e[1;33m ${assArray2[*]} \e[0m";
- echo -e "\e[1;32mObtain all elements of ordinary array assArray2 with \"\${array_var1[@]}\" are:\n \e[1;33m ${assArray2[@]} \e[0m";
- echo -e "\e[1;32mObtain Length of array assArray2 with \"\${assArray2[*]}\" is: \e[1;33m${#assArray2[*]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray2 with \"\${assArray2[@]}\" is: \e[1;33m${#assArray2[@]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray2[pear] with \"\${#assArray2[pear]}\" is: \e[1;33m${#assArray2[pear]}\e[0m";
- echo -e "\e[1;32mObtain Length of array assArray2[apple] with \"\${#assArray2[\$index]}\" is: \e[1;33m${#assArray2[$index]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array assArray2 with \"\${!assArray2[*]} are:\n \e[1;33m${!assArray2[*]}\e[0m";
- echo -e "\e[1;32mObtain all indexes of array assArray2 with \"\${!assArray2[@]} are:\n \e[1;33m${!assArray2[@]}\e[0m";
关联数组与常规数组访问的唯一区别就是使用字符串作为索引值;
上面实例代码值得一提的是,通过 "#arrayName[index]" 方式可以获得元素的字符串长度;
呵呵!这就是我理解的 Linux Shell 里面的数组,有误之处,欢迎交流!