Shell中的数组
shell中的数组有增删改查的功能,但是增删改的功能很少用,查是最常用的,一般和for循环相结合
Last login: Wed Nov 25 17:24:29 2015 from 192.168.220.1 [root@lamp ~]# kobe=(k o b e) [root@lamp ~]# echo ${#kobe(@)} -bash: ${#kobe(@)}: bad substitution [root@lamp ~]# echo ${#kobe[@]} 4 [root@lamp ~]# echo ${#kobe[*]} 4 [root@lamp ~]# kobe[4]=k [root@lamp ~]# echo ${#kobe[@]} 5 [root@lamp ~]# echo ${kobe[0]} k [root@lamp ~]# echo ${kobe[1]} o [root@lamp ~]# echo ${kobe[4]} k [root@lamp ~]# kobe[0]=kk [root@lamp ~]# echo ${#kobe[@]} 5 [root@lamp ~]# echo ${kobe[0]} kk [root@lamp ~]# unset kobe [root@lamp ~]# echo $kobe [root@lamp ~]# kobe=(k o b e) [root@lamp ~]# unset ${kobe[0]} [root@lamp ~]# echo $kobe k [root@lamp ~]# unset ${#kobe[*]} -bash: unset: `4': not a valid identifier [root@lamp ~]# unset ${kobe[@]} [root@lamp ~]# kobe=(k o b e) [root@lamp ~]# unset ${kobe[0]} [root@lamp ~]# echo ${kobe[@]} k o b e [root@lamp ~]# echo kobe[@] kobe[@] [root@lamp ~]# unset kobe[@] [root@lamp ~]# unset kobe[0] [root@lamp ~]# echo ${kobe[@]} [root@lamp ~]# kobe=(k o b e) [root@lamp ~]# unset ${kobe[@]} [root@lamp ~]# echo ${kobe[@]} k o b e [root@lamp ~]# unset kobe[0] [root@lamp ~]# echo ${kobe[@]} o b e [root@lamp ~]#
数组的应用
[root@lamp ~]# ls EOF install.log robots.txt test anaconda-ks.cfg install.log.syslog tar.sh [root@lamp ~]# file=($(ls)) [root@lamp ~]# echo ${file[@]} EOF anaconda-ks.cfg install.log install.log.syslog robots.txt tar.sh test [root@lamp ~]# echo ${#file[*]} 7 [root@lamp ~]# for((i=0;i<${#file[@]};i++));do echo ${file[$i]};done EOF anaconda-ks.cfg install.log install.log.syslog robots.txt tar.sh test [root@lamp ~]# [root@lamp ~]# for n in ${file[@]};do echo $n;done EOF anaconda-ks.cfg install.log install.log.syslog robots.txt tar.sh test [root@lamp ~]#