shell之 数组 bash shell只支持一维。数组从0开始,以array[x]表示数组元素。array[0]表示第一个元素。bash shell 支持最大数组标号是599 147 937 791。获取bash shell数组值的方式 ${array[x]}。(一)bash shell 数组常见用法:1、bash shell脚本执行权限2、数组赋值和获取数组值3、圆括号对数组赋值4、圆括号赋值且指定元素值5、@和*表示数组元素6、@和*加引号打印区别(二)bash shell 数组特殊用法1、抽取、删除和替换数组元素中的子串2、声明数组、清空数组、求取数组长度3、数组与数组连接(三)bash shell 实现数据结构bash shell不直接支持堆栈和二维数组,但是可以通过一维数组来实现这些线性数据结构。1、实现堆栈2、实现二维数组
(一)bash shell 数组常见用法:
1、bash shell脚本执行权限[root@rhel6 ~]# mkdir /zxx_shell[root@rhel6 ~]# chown -R oracle:oinstall /zxx_shell/[root@rhel6 ~]# su - oracle[oracle@rhel6 ~]$ cd /zxx_shell/[oracle@rhel6 zxx_shell]$ vi 1-array.sh[oracle@rhel6 zxx_shell]$ ll总用量 4-rw-r--r--. 1 oracle oinstall 168 8月 4 17:00 1-array.sh[oracle@rhel6 zxx_shell]$ chmod +x *[oracle@rhel6 zxx_shell]$ ll总用量 4-rwxr-xr-x. 1 oracle oinstall 168 8月 4 17:00 1-array.shshell脚本需要执行权限
2、数组赋值和获取数组值[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx[0]=wiscom0clxx[1]=0256656clxx[8]=wiscom8echo "clxx[0]=${clxx[0]}"echo "clxx[1]=${clxx[1]}"echo "clxx[8]=${clxx[8]}"echo "clxx[20]=${clxx[20]}"[oracle@rhel6 zxx_shell]$ ./1-array.shclxx[0]=wiscom0clxx[1]=0256656clxx[8]=wiscom8clxx[20]=允许数组空缺元素,也可以不连续的给数组赋值。
3、圆括号对数组赋值[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=(wiscom0 0256656 wiscom8)echo "clxx[0]=${clxx[0]}"echo "clxx[1]=${clxx[1]}"echo "clxx[2]=${clxx[2]}"echo "clxx[3]=${clxx[3]}"echo "clxx[8]=${clxx[8]}"echo "clxx[20]=${clxx[20]}"[oracle@rhel6 zxx_shell]$ ./1-array.shclxx[0]=wiscom0clxx[1]=0256656clxx[2]=wiscom8clxx[3]=clxx[8]=clxx[20]=圆括号默认从0开始,默认以空格为分隔符。
4、圆括号赋值且指定元素值[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=(wiscom0 [3]=0256656 wiscom8)echo "clxx[0]=${clxx[0]}"echo "clxx[1]=${clxx[1]}"echo "clxx[2]=${clxx[2]}"echo "clxx[3]=${clxx[3]}"echo "clxx[4]=${clxx[4]}"echo "clxx[8]=${clxx[8]}"echo "clxx[20]=${clxx[20]}"[oracle@rhel6 zxx_shell]$ ./1-array.shclxx[0]=wiscom0clxx[1]=clxx[2]=clxx[3]=0256656clxx[4]=wiscom8clxx[8]=clxx[20]=可以元素位置从指定元素重新计算,如果指定元素之后值没有指定元素[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)echo "clxx[0]=${clxx[0]}"echo "clxx[1]=${clxx[1]}"echo "clxx[2]=${clxx[2]}"echo "clxx[3]=${clxx[3]}"echo "clxx[4]=${clxx[4]}"echo "clxx[8]=${clxx[8]}"echo "clxx[20]=${clxx[20]}"[oracle@rhel6 zxx_shell]$ ./1-array.shclxx[0]=wiscom8clxx[1]=clxx[2]=clxx[3]=0256656clxx[4]=clxx[8]=wiscom0clxx[20]=
5、@和*表示数组元素[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)echo "clxx[@]=${clxx[@]}"echo "----------------"echo "clxx[*]=${clxx[*]}"[oracle@rhel6 zxx_shell]$ ./1-array.shclxx[@]=wiscom8 0256656 wiscom0----------------clxx[*]=wiscom8 0256656 wiscom0@和*表示数组元素表示数组所有元素。[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)for i in ${clxx[@]}do echo $idone [oracle@rhel6 zxx_shell]$ ./1-array.shwiscom80256656wiscom0打印时只输出赋值的元素。
6、@和*加引号打印区别注意:当使用引号时,@和*打印有区别[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)for i in "${clxx[@]}"do echo $idone [oracle@rhel6 zxx_shell]$ ./1-array.shwiscom80256656wiscom0[oracle@rhel6 zxx_shell]$ vi 1-array.sh[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=([8]=wiscom0 [3]=0256656 [0]=wiscom8)for i in "${clxx[*]}"do echo $idone [oracle@rhel6 zxx_shell]$ ./1-array.shwiscom8 0256656 wiscom0
(二)bash shell 数组特殊用法
1、抽取、删除和替换 数组元素中的子串[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashclxx=(wsscom0 0256656 wiscom888 ednns)echo "extracting substring"echo ${clxx[*]:0} #表示从第一个元素开始抽取echo ${clxx[*]:1} #表示从第二个元素开始抽取echo ${clxx[*]:3}echo ${clxx[*]:0:1} #表示从第一个元素开始抽取2个元素echo "removing substring"echo ${clxx[*]#w*s} #表示删除元素中匹配w*s的子串echo "replacing substring"echo ${clxx[*]/w*s/zxx} #表示第一次全部将元素中匹配w*s的子串替换为"zxx"子串echo ${clxx[*]//s*c/cs} #表示全部将元素中匹配s*c的子串替换为"cs"子串,如果第一次替换之后又符合“s*c”又进行替换,直到没有为止[oracle@rhel6 zxx_shell]$ ./1-array.shextracting substringwsscom0 0256656 wiscom888 ednns0256656 wiscom888 ednnsednnswsscom0removing substringscom0 0256656 com888 ednnsreplacing substringzxxcom0 0256656 zxxcom888 ednnswcsom0 0256656 wicsom888 ednns
2、声明数组、清空数组、求取数组长度[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashdeclare -a clxx #声明clxx为一个数组echo "input company by a SPACE" #元素之间需要用空格隔离read -a clxx #将键盘输入的值赋值给clxx数组for i in $ "${clxx[*]}"doecho "$i"doneecho "the length of this array clxx is:${#clxx[*]}" #获取数组长度unset clxx[1] #清除clxx[1]元素echo "the length of this array clxx is:${#clxx[*]}"unset clxx #清除clxx整个数组echo "the length of this array clxx is:${#clxx[*]}"[oracle@rhel6 zxx_shell]$ ./1-array.shinput company by a SPACEwiscom ednns zxx wisedu$wiscom ednns zxx wiseduthe length of this array clxx is:4the length of this array clxx is:3the length of this array clxx is:0[oracle@rhel6 zxx_shell]$ vi 1-array.sh[oracle@rhel6 zxx_shell]$ ./1-array.shinput company by a SPACEwiscom ednns zxx wisedu$wiscom ednns zxx wiseduthe length of this array clxx is:4the length of this array clxx is:3the length of this array clxx is:0
3、数组与数组连接[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashcompony=(wiscom wisvision wisedu)person=(zxx [7]=wl yxz)declare -a combine #声明combine为数组combine=(${compony[*]} ${person[*]}) #连接两个数组组成combine数组element_count=${#combine[*]}index=0while [ "$index" -lt "$element_count" ] #while循环遍历do echo "element[$index]=${combine[$index]}" let "index=$index+1" doneechounset combine #清空数组combine[0]=${compony[*]}combine[1]=${person[*]}element_count=${#combine[*]}index=0while [ "$index" -lt "$element_count" ]do echo "element[$index]=${combine[$index]}" let "index=$index+1"done[oracle@rhel6 zxx_shell]$ ./1-array.shelement[0]=wiscomelement[1]=wisvisionelement[2]=wiseduelement[3]=zxxelement[4]=wlelement[5]=yxzelement[0]=wiscom wisvision wiseduelement[1]=zxx wl yxz第一种连接会将子数组中的不连接元素按顺序重新赋值给连接数组第二种连接直接输类似赋值
(三)bash shell 实现数据结构
1、实现堆栈[oracle@rhel6 zxx_shell]$ cat 1-array.sh#!/bin/bashmaxtop=50 #堆栈放入最大元素top=$maxtop #定义栈顶指针,初始值为maxtoptemp= #定义临时全局变量,存放出栈元素declare -a stack #定义一个全局数组stackpush(){if [ -z "$1" ] #如果第一参数为空,退出push函数then returnfiuntil [ $# -eq 0 ] #如果参数个数为0退出do let top=top-1 stack[$top]=$1 shift #将后面参数逐个覆盖前面参数donereturn}#push是将第一个参数赋值给stack[49],第二个参数赋值给stack[48],依次循环pop(){temp=if [ "$top" -eq "$maxtop" ] #如果堆栈为空退出popthen returnfitemp=${stack[$top]} #将栈顶赋值给tempunset stack[$top]let top=top+1return}#pop是将栈顶赋值给temp,然后清空栈顶status(){echo "===============stack===================="for i in ${stack[*]}do echo $idoneecho "------------------------------------"echo "stack pointer=$top"echo "just popped \""$temp"\" off the stack"echo "=========================================="}push wiscomstatuspush ednns wisvision wisedustatuspoppopstatuspush zxxpush zxx kkkstatus[oracle@rhel6 zxx_shell]$ ./1-array.sh===============stack====================wiscom------------------------------------stack pointer=49just popped "" off the stack=========================================================stack====================wiseduwisvisionednnswiscom------------------------------------stack pointer=46just popped "" off the stack=========================================================stack====================ednnswiscom------------------------------------stack pointer=48just popped "wisvision" off the stack=========================================================stack====================kkkzxxzxxednnswiscom------------------------------------stack pointer=45just popped "wisvision" off the stack==========================================第一个参数为: $1 参数个数为:$#shift原来的$2会变成$1
2、实现二维数组