shell数组小结
来源:互联网 发布:五五开笑笑知乎 编辑:程序博客网 时间:2024/06/09 18:18
数组作为一种特殊的数据结构在任何一种编程语言中都有它的一席之地,当然bash shell也不例外。本文就shell数组来做一个小的总结。
在这里只讨论一维数组的情况,关于多维数组(事实上,你得用一维数组的方法来模拟),不涉及。这里包括数组的复制,计算,删除,替换。
数组的声明:数组的访问: 数组的删除 计算数组的长度: 数组的提取
从尾部开始提取:
array=( [0]=one [1]=two [2]=three [3]=four )
${array[@]:1} # two three four,除掉第一个元素后所有元素,那么${array[@]:0}表示所有元素
${array[@]:0:2} # one two
${array[@]:1:2} # two three
子串删除子串替换 第一个匹配到的,会被删除 所有匹配到的,都会被删除 没有指定替换子串,则删除匹配到的子符 替换字符串前端子串 替换字符串后端子串
在这里只讨论一维数组的情况,关于多维数组(事实上,你得用一维数组的方法来模拟),不涉及。这里包括数组的复制,计算,删除,替换。
数组的声明:
- 1)array[key]=value # array[0]=one,array[1]=two
- 2)declare -a array # array被当作数组名
- 3)array=( value1 value2 value3 ... )
- 4)array=( [1]=one [2]=two [3]=three ... )
- 5)array="one two three" # echo ${array[0|@|*]},把array变量当作数组来处理,但数组元素只有字符串本身
- 1)${array[key]} # ${array[1]}
- 1)unset array[1] # 删除数组中第一个元素
- 2)unset array # 删除整个数组
- 1)${#array}
- 2)${#array[0]} #同上。 ${#array[*]} 、${#array[@]}。注意同#{array:0}的区别
从尾部开始提取:
array=( [0]=one [1]=two [2]=three [3]=four )
${array[@]:1} # two three four,除掉第一个元素后所有元素,那么${array[@]:0}表示所有元素
${array[@]:0:2} # one two
${array[@]:1:2} # two three
子串删除
- [root@localhost dev]# echo ${array[@]:0}
- one two three four
- [root@localhost dev]# echo ${array[@]#t*e} # 左边开始最短的匹配:"t*e",这将匹配到"thre"
- one two e four
- [root@localhost dev]# echo ${array[@]##t*e} # 左边开始最长的匹配,这将匹配到"three"
- [root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )
- [root@localhost dev]# echo ${array[@] %o} # 从字符串的结尾开始最短的匹配
- one tw three four
- [root@localhost dev]# echo ${array[@] %%o} # 从字符串的结尾开始最长的匹配
- one tw three four
- [root@localhost dev]# array=( [0]=one [1]=two [2]=three [3]=four )
- [root@localhost dev]# echo ${array[@] /o/m}
- mne twm three fmur
- [root@localhost dev]# echo ${array[@] //o/m}
- mne twm three fmur
- [root@localhost dev]# echo ${array[@] //o/}
- ne tw three fur
- [root@localhost dev]# echo ${array[@] /#o/k}
- kne two three four
- [root@localhost dev]# echo ${array[@] /%o/k}
- one twk three four
转自
http://bbs.chinaunix.net/thread-1779167-1-1.html
- shell数组小结
- shell数组小结
- shell数组小结
- shell数组小结
- shell数组小结
- shell数组小结
- shell数组小结
- Shell脚本数组操作小结
- Shell脚本数组操作小结
- Shell脚本数组操作小结
- Shell小结
- shell小结
- shell小结
- 数组小结
- 数组小结
- 数组小结
- 数组小结
- 数组小结
- pageEncoding与contentType的区别;在servlet中编写html页面显示必要条件
- Android开发10.2:UI组件AutoCompleteTextView(自动完成文本框)
- hdu_3127 WHUgirls
- Intent传递数据时,可以传递哪些类型数据?
- 高性能计算相关资源
- shell数组小结
- hdu_1028 Ignatius and the Princess III
- HDU1009 FatMouse' Trade
- android中判断网络环境
- 解读google的一道面试题
- SQL日期类型
- poj_3181 Dollar Dayz
- 【编程思考】如何使用叛逆的goto ?
- hdu_3339 In Action 最短路径+dp