Bash Arrays
来源:互联网 发布:网络大电影数据分析 编辑:程序博客网 时间:2024/06/05 21:15
If you're used to a "standard" *NIX shell you may not be familiar with bash's array feature. Although not as powerful as similar constructs in the P languages (Perl, Python, and PHP) and others, they are often quite useful.
Bash arrays have numbered indexes only, but they are sparse, ie you don't have to define all the indexes. An entire array can be assigned by enclosing the array items in parenthesis:
arr=(Hello World)Individual items can be assigned with the familiar array syntax (unless you're used to Basic or Fortran):
arr[0]=Hello arr[1]=WorldBut it gets a bit ugly when you want to refer to an array item:
echo ${arr[0]} ${arr[1]}To quote from the man page:
The braces are required to avoid conflicts with pathname expansion.
In addition the following funky constructs are available:
${arr[*]} # All of the items in the array ${!arr[*]} # All of the indexes in the array ${#arr[*]} # Number of items in the array ${#arr[0]} # Length of item zeroThe ${!arr[*]} is a relatively new addition to bash, it was not part of the original array implementation.
The following example shows some simple array usage (note the "[index]=value" assignment to assign a specific index):
#!/bin/basharray=(one two three four [5]=five)echo "Array size: ${#array[*]}"echo "Array items:"for item in ${array[*]}do printf " %s/n" $itemdoneecho "Array indexes:"for index in ${!array[*]}do printf " %d/n" $indexdoneecho "Array items and indexes:"for index in ${!array[*]}do printf "%4d: %s/n" $index ${array[$index]}done
Array size: 5Array items: one two three four fiveArray indexes: 0 1 2 3 5Array items and indexes: 0: one 1: two 2: three 3: four 5: five
Note that the "@" sign can be used instead of the "*" in constructs such as ${arr[*]}, the result is the same except when expanding to the items of the array within a quoted string. In this case the behavior is the same as when expanding "$*" and "$@" within quoted strings: "${arr[*]}" returns all the items as a single word, whereas "${arr[@]}" returns each item as a separate word.
The following example shows how unquoted, quoted "*", and quoted "@" affect the expansion (particularly important when the array items themselves contain spaces):
#!/bin/basharray=("first item" "second item" "third" "item")echo "Number of items in original array: ${#array[*]}"for ix in ${!array[*]}do printf " %s/n" "${array[$ix]}"doneechoarr=(${array[*]})echo "After unquoted expansion: ${#arr[*]}"for ix in ${!arr[*]}do printf " %s/n" "${arr[$ix]}"doneechoarr=("${array[*]}")echo "After * quoted expansion: ${#arr[*]}"for ix in ${!arr[*]}do printf " %s/n" "${arr[$ix]}"doneechoarr=("${array[@]}")echo "After @ quoted expansion: ${#arr[*]}"for ix in ${!arr[*]}do printf " %s/n" "${arr[$ix]}"done
Number of items in original array: 4 first item second item third itemAfter unquoted expansion: 6 first item second item third itemAfter * quoted expansion: 1 first item second item third itemAfter @ quoted expansion: 4 first item second item third item
__________________________
- Bash Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- Arrays
- SQL server 2005 怎样配置SA
- COM 学习笔记(1)
- 论文下载点整理
- 看看程序员是怎么读简历的(图)
- JQuery each迭代Table中某列值
- Bash Arrays
- Web.config文件例子详解
- POJ 2074 Line of Sight
- 对初学单片机的同学们的几点建议
- GDI与DX截屏API操作
- 谁知道如下这样报表是用水晶报表做的吗,如不是水晶报表做的,又是如何做的呢,
- 用Visual C#创建Windows服务程序
- 获取本机公网上的IP地址(借助发布在公网上的IP查询网站)
- 人力大量输出:中国距劳动力短缺时代还有多远?