shell中的数组和列表
一、列表(list)和数组(array)
1、seq
root@localhost:/appfs# seq 5 //起始默认是1,间隔默认也是1 1 2 3 4 5 root@localhost:/appfs# alist=$(seq 5) //alist得到的是字符串,不同之处是以空格隔开 root@localhost:/appfs# echo $alist 1 2 3 4 5 root@localhost:/appfs# for i in $alist; do echo $i; done; //可看成是list,用for...in循环读取 1 2 3 4 5
root@localhost:/appfs# echo {1..5} //通过内部{begin..end}生成,性能比seq快 1 2 3 4 5 root@localhost:/appfs# for i in {1..5}; do echo $i; done 1 2 3 4 5
2、array
root@localhost:/appfs# arry=($alist) //如果需要生成array,只需要将$(seq 5)再加个“()”即可 root@localhost:/appfs# echo $arry 1 root@localhost:/appfs# echo ${arry[0]} 1 root@localhost:/appfs# echo ${arry[1]} 2 root@localhost:/appfs# echo ${arry[3]} 4 root@localhost:/appfs# echo ${arry[@]} 1 2 3 4 5 root@localhost:/appfs# for i in ${arry[@]}; do echo $i; done 1 2 3 4 5