来源:http://www.linuxjournal.com/content/bash-arrays
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/bash
array=(one two three four [5]=five)
echo "Array size: ${#array[*]}"
echo "Array items:"
for item in ${array[*]}
do
printf " %s\n" $item
done
echo "Array indexes:"
for index in ${!array[*]}
do
printf " %d\n" $index
done
echo "Array items and indexes:"
for index in ${!array[*]}
do
printf "%4d: %s\n" $index ${array[$index]}
done
Array size: 5 Array items: one two three four five Array indexes: 0 1 2 3 5 Array 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/bash
array=("first item" "second item" "third" "item")
echo "Number of items in original array: ${#array[*]}"
for ix in ${!array[*]}
do
printf " %s\n" "${array[$ix]}"
done
echo
arr=(${array[*]})
echo "After unquoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${array[*]}")
echo "After * quoted expansion: ${#arr[*]}"
for ix in ${!arr[*]}
do
printf " %s\n" "${arr[$ix]}"
done
echo
arr=("${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 item After unquoted expansion: 6 first item second item third item After * quoted expansion: 1 first item second item third item After @ quoted expansion: 4 first item second item third item
Mitch Frazier is an Associate Editor for Linux Journal.
相关推荐
Help for Power Users and Sys Admins It’s simple: if you want to interact deeply with Mac OS X, Linux, and other Unix-like systems, you need to know how to work with the Bash shell. This concise ...
总的来说,《Advanced Bash-Scripting Guide》是一本全面、深入的教程,适合任何想要提升Bash脚本技能的Linux/UNIX用户,无论你是初学者还是经验丰富的系统管理员,都能从中受益匪浅。通过学习这本书,你可以掌握...
You need to know how to work with the bash shell if you want to get to the heart of Unix systems, including Linux and Mac OS X. Now covering the most recent version of bash, this concise little book ...
同时,提供了一系列的并行数组操作方法,如`Arrays.parallelSort()`。 6. **NIO.2 (New IO 2.0)**: 添加了新的文件系统API,支持异步I/O操作,提供更好的文件操作能力。 7. **Binary Literals and Underscore in ...
2. **多路归并排序**:Java 7中的Arrays.sort()方法采用了新的多路归并排序算法,提升了数组排序的性能。 3. **Try-with-resources语句**:这是一种新的异常处理结构,允许开发者在try语句块中声明资源,资源会在...
对于Linux用户,这通常通过修改`.bashrc`或`.bash_profile`文件完成。完成安装后,可以通过`java -version`命令检查JDK是否正确安装并配置。 在开发环境中,JDK 8不仅提供基础的Java运行环境,还是许多现代Java应用...
8. **并行数组操作**:`java.util.Arrays`类和`java.util.Collections`类增加了新的并行操作,如`parallelSort()`,利用多核处理器提高排序速度。 安装JDK 8u211在Linux系统上的步骤通常包括解压文件、配置环境变量...
具体步骤可能因不同的Linux发行版而异,但通常包括使用`unzip`命令解压文件,将其移动到 `/usr/lib/jvm` 或其他指定的软件安装目录,并编辑`~/.bashrc`或`~/.bash_profile`文件以更新环境变量。 总之,`jdk1.7.0_80...
Judy这个名字可能是从Judy arrays(一种高效的数据结构)借鉴的,但具体的功能和用法需要查看库的文档或源代码才能了解。 在使用此库之前,开发者通常会使用`pip`工具进行安装,命令可能是: ```bash pip install ...
8. **并行数组操作**:Arrays类和Parallel Streams提供了并行化的数组操作,可以利用多核处理器提高性能。 **JDK 1.8.0_45更新内容:** JDK 1.8.0_45是Java 8的一个安全更新版本,主要修复了一些已知的安全漏洞,...
awkward是一个Python库,专注于处理不规则或“尴尬”的数据,如 Jagged Arrays(数组的数组)和 Record Arrays(记录数组),这些数据结构在处理复杂数据集时非常有用,尤其是在科学计算和数据分析领域。 在Python...
它提供了一种类似于NumPy的API,但能处理复杂的数据结构,如 Jagged Arrays(嵌套数组)和 Record Arrays(带有命名字段的数组)。这使得Awkward在处理JSON、CSV等复杂数据格式时表现出色,尤其是在科学数据分析、...
- 可以使用bash、sh等Shell编写。 #### 四、负载均衡与分布式存储 - **LVS负载均衡** - LVS是一种基于Linux内核的负载均衡解决方案。 - 支持多种负载均衡算法。 - **F5负载均衡器** - F5是一款高性能的硬件...
8. **数组(Arrays)**:尽管Bash中的数组功能相对简单,但仍然可以用来存储一组相关的值,如`myarray=(value1 value2 value3)`。 9. **命令别名(Aliases)**:为了简化常用命令,可以定义命令别名,如`alias ll='...
在系统的环境变量配置文件(如 Linux 的 `~/.bashrc` 或 Windows 的 `Environment Variables`)中添加 Kafka 相关路径: ```bash export KAFKA_HOME=/usr/local/kafka export PATH=$PATH:$KAFKA_HOME/bin ``` 执行...
3. 操作系统:Linux或Windows,推荐使用Linux,因为其更好的稳定性和性能。 二、安装Solr 1. 下载最新版的Solr,例如Solr 8.x,可以从Apache官网下载。 2. 解压下载的Solr压缩包到服务器的适当目录,例如 `/opt/...
bash: javac: command not found ``` 这表明尽管 `java -version` 显示了某个 Java 版本,但 `javac` 编译器并未被正确配置。因此,我们不能仅依赖 `java -version` 来判断 JDK 是否安装成功。 #### 二、安装 JDK8...
`Judy`这个名字可能来源于一种高效的数据结构,例如Judy arrays,这是一种稀疏数组实现,以极高的空间效率和快速的查找速度著称。 **Python版本与兼容性** `cp36-cp36m`这部分表示这个库是为Python 3.6编译的,...
而在Linux或macOS中,这些设置可能需要在`~/.bashrc`或`~/.bash_profile`文件中完成。 总的来说,JDK 1.8.0_201作为Java 8的一个更新,它不仅提高了开发者的工作效率,也提供了更好的性能和安全性,对于Java开发者...