shell脚本中数组的使用_Shell脚本中的数组

shell脚本中数组的使用

Knowing how to work with arrays in shell scripts will help you work with larger datasets in a much efficient manner. But what are arrays and how can you create arrays? Let’s find out!

了解如何在Shell脚本中使用数组将帮助您以高效的方式处理较大的数据集。 但是什么是数组,如何创建数组? 让我们找出答案!

什么是数组? (What are Arrays?)

If you already have a basic understanding of any programming language, you know what arrays are. But for the uninitiated, let’s go over the basics of arrays and learn how to work with them.

如果您已经对任何编程语言都有基本的了解,那么您就知道什么是数组。 但是,对于那些没有经验的人,让我们研究一下数组的基础知识,并学习如何使用它们。

Variables store single data elements. Arrays, on the other hand, can store a virtually unlimited number of data elements. When working with a large amount of data, variables can prove to be very inefficient and it’s very helpful to get hands-on with arrays.

变量存储单个数据元素。 另一方面,阵列可以存储几乎无限数量的数据元素。 当处理大量数据时,变量可能证明效率很低,并且动手操作数组非常有帮助。

Let’s learn how to create arrays in shell scripts.

让我们学习如何在shell脚本中创建数组。

在Shell脚本中创建数组 (Creating Arrays in Shell Scripts)

There are two types of arrays that we can work with, in shell scripts.

在shell脚本中,可以使用两种类型的数组。

  • Indexed Arrays – Store elements with an index starting from 0

    索引数组 –存储索引从0开始的元素
  • Associative Arrays – Store elements in key-value pairs

    关联数组 –将元素存储在键值对中

The default array that’s created is an indexed array. If you specify the index names, it becomes an associative array and the elements can be accessed using the index names instead of numbers.

创建的默认数组是索引数组。 如果指定索引名称,则它将成为一个关联数组,并且可以使用索引名称而不是数字来访问元素。

Declaring Arrays:

声明数组:


root@ubuntu:~# declare -A assoc_array
root@ubuntu:~# assoc_array[key]=value
OR
root@ubuntu:~# declare -a indexed_array
root@ubuntu:~# indexed_array[0]=value

Notice the uppercase and lowercase letter a. Uppercase A is used to declare an associative array while lowercase a is used to declare an indexed array.

注意大小写字母a 。 大写字母A用于声明关联数组,而小写字母a用于声明索引数组。

The declare keyword is used to explicitly declare arrays but you do not really need to use them. When you’re creating an array, you can simply initialize the values based on the type of array you want without explicitly declaring the arrays.

declare关键字用于显式声明数组,但您实际上并不需要使用它们。 创建数组时,您可以简单地根据所需的数组类型初始化值,而无需显式声明数组。

在Shell脚本中使用数组 (Working with Arrays in Shell Scripts)

Now that you know how to create arrays, let’s learn how to work with arrays. Since these are collections of data elements, we can work with loops and arrays at the same time to extract the required data points.

现在您知道如何创建数组,让我们学习如何使用数组。 由于这些是数据元素的集合,因此我们可以同时使用循环和数组来提取所需的数据点。

1.分别访问数组元素 (1. Accessing Array Elements Individually)

Since we know that each data point is being indexed individually, we can access all the array elements by specifying the array index as shown below:

既然我们知道每个数据点都是单独索引的,则可以通过指定数组索引来访问所有数组元素,如下所示:


assoc_array[element1]="Hello World"
echo ${assoc_array[element1]}
Associative Arrays In Shell Script
Associative Arrays In Shell Script
Shell脚本中的关联数组

Similarly, let’s access some indexed array elements. We can specify all the elements for the index array by delimiting with spaces because the index is automatically generated for each of those elements.

同样,让我们​​访问一些索引数组元素。 我们可以用空格分隔来指定索引数组的所有元素,因为索引是为每个元素自动生成的。


index_array=(1 2 3 4 5 6)
echo ${index_array[0]}
Index Arrays In Shell Scripts
Index Arrays In Shell Scripts
Shell脚本中的索引数组

As you can see, the first element is automatically printed based on index 0.

如您所见,第一个元素将基于索引0自动打印。

2.顺序读取数组元素 (2. Reading Array Elements Sequentially)

This is going to be an easy task if you know for loops already. If you don’t we’ll cover them in a future tutorial. We’ll make use of the while or for loops in shell scripts to work through the array elements. Copy the script below and save it as <filename>.sh

如果您已经知道for循环,这将是一个简单的任务。 如果您不这样做,我们将在以后的教程中介绍它们。 我们将利用shell脚本中的while或for循环遍历数组元素。 复制下面的脚本并将其另存为<filename> .sh


#!/bin/bash
index_array=(1 2 3 4 5 6 7 8 9 0)

for i in ${index_array[@]}
do
        echo $i
done

The above script will output the following:

上面的脚本将输出以下内容:

Looping Over Arrays In Shell Scripts
Looping Over Arrays In Shell Scripts
在Shell脚本中循环遍历数组

Now you might have noticed the index_array[@] and if you’re wondering what the @ symbol is for, we’re going to go over the same right now.

现在您可能已经注意到index_array [@]了 ,如果您想知道@符号是什么,我们现在将再次讨论。

Shell脚本中数组的内置操作 (Built-in Operations for Arrays in Shell Scripts)

Now that you learned how to access elements individually and using for loops, let’s learn the different operations that are available by default for arrays.

既然您已经了解了如何分别访问元素和使用for循环,那么我们将学习默认情况下可用于数组的不同操作。

1.访问数组的所有元素 (1. Access All Elements of an Array)

We learned how to access elements by providing the index or the key of the array. But if we want to print all the elements at the same time or work with all the elements, we can use another operator which is the [@] symbol.

我们学习了如何通过提供数组的索引或键来访问元素。 但是,如果我们要同时打印所有元素或使用所有元素,则可以使用另一个运算符,即[@]符号。

As you noticed in the example above, I used this symbol when I wanted to loop through all the array elements using the for loop.

正如您在上面的示例中注意到的那样,当我想使用for循环遍历所有数组元素时,使用了此符号。


echo ${assoc_array[@]}

The above will print all the elements that are stored within the assoc array.

上面的代码将打印存储在assoc数组中的所有元素。

2.计算数组中的元素数 (2. Count the Number of Elements in an Array)

Similar to the @ symbol above, we have the # symbol which can be prefixed to an array name to provide us the count of the elements stored in the array. Let’s see how it works.

与上面的@符号类似,我们使用#符号,该符号可以加到数组名称的前面,以向我们提供存储在数组中的元素的数量。 让我们看看它是如何工作的。


echo ${#index_array[@]}

If you want to count the number of characters used for a particular element, we can simply replace the @ symbol with the index.

如果要计算用于特定元素的字符数,我们可以简单地将@符号替换为索引。

3.删除单个数组元素 (3. Delete Individual Array Elements)

We know how to add array elements and print them too. Let’s learn how to delete specific elements. For this purpose, we’ll use the unset keyword.

我们知道如何添加数组元素并打印它们。 让我们学习如何删除特定元素。 为此,我们将使用unset关键字。


unset index_array[1]

Replace the array name and the index ID in the above code example and you’ve removed the array element that you desire. Pretty simple isn’t it?

在上面的代码示例中,替换数组名称和索引ID,并删除了所需的数组元素。 很简单,不是吗?

结论 (Conclusion)

Shell scripts are pretty vast and can replace any function that you can perform on the terminal with the right person writing the script. Some additional functionalities of arrays in shell scripts also include being able to work with regex (Regular Expressions). We can use various regular expressions to manipulate array elements within shell scripts.

Shell脚本非常庞大,可以由合适的人编写脚本来替换您可以在终端上执行的任何功能。 Shell脚本中数组的一些其他功能还包括能够使用regex (正则表达式)。 我们可以使用各种正则表达式来操作Shell脚本中的数组元素。

For now, we hope you have a good understanding of creating and working with arrays and will be able to use arrays in your scripting. Comment below to let us know what you think, and if you have any questions about this topic.

目前,我们希望您对创建和使用数组有一个很好的了解,并能够在脚本中使用数组。 请在下面发表评论,以使我们知道您的想法,以及对此主题有任何疑问。

翻译自: https://www.journaldev.com/35945/arrays-in-shell-scripts

shell脚本中数组的使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值