Unix 简明教程

Unix / Linux - Using Shell Arrays

在本章中,我们将讨论如何在 Unix 中使用 shell 数组。shell 变量有足够的能力来保存单个值。这些变量称为标量变量。

In this chapter, we will discuss how to use shell arrays in Unix. A shell variable is capable enough to hold a single value. These variables are called scalar variables.

Shell 支持一种不同的变量类型,称为 array variable 。这可以同时保存多个值。数组提供了一种对变量集合进行分组的方法。你可以使用一个保存所有其他变量的单一数组变量,而不必为每个必需的变量创建一个新名称。

Shell supports a different type of variable called an array variable. This can hold multiple values at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a new name for each variable that is required, you can use a single array variable that stores all the other variables.

为命名数组时,所有讨论过的 Shell 变量的命名规则均适用。

All the naming rules discussed for Shell Variables would be applicable while naming arrays.

Defining Array Values

数组变量和标量变量之间的差异可以解释如下。

The difference between an array variable and a scalar variable can be explained as follows.

假设你要将各种学生的姓名表示为一组变量。每个单独变量都是一个标量变量,如下所示 –

Suppose you are trying to represent the names of various students as a set of variables. Each of the individual variables is a scalar variable as follows −

NAME01="Zara"
NAME02="Qadir"
NAME03="Mahnaz"
NAME04="Ayan"
NAME05="Daisy"

我们可以使用一个单独的数组来存储所有上面提到的姓名。以下是创建数组变量的最简单方法。这有助于将其一个索引值分配给其一个索引。

We can use a single array to store all the above mentioned names. Following is the simplest method of creating an array variable. This helps assign a value to one of its indices.

array_name[index]=value

此处,array_name 是数组的名称,index 是你要设置的数组中该项的索引,而 value 是你要为该项设置的值。

Here array_name is the name of the array, index is the index of the item in the array that you want to set, and value is the value you want to set for that item.

例如,以下命令 –

As an example, the following commands −

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"

如果你使用的是 ksh shell,则以下为数组初始化的语法 –

If you are using the ksh shell, here is the syntax of array initialization −

set -A array_name value1 value2 ... valuen

如果你使用的是 bash shell,则以下为数组初始化的语法 –

If you are using the bash shell, here is the syntax of array initialization −

array_name=(value1 ... valuen)

Accessing Array Values

在你设置任何数组变量后,访问方式如下 –

After you have set any array variable, you access it as follows −

${array_name[index]}

此处,array_name 是数组的名称,而 index 是要访问的值的索引。以下是一个了解这个概念的例子 –

Here array_name is the name of the array, and index is the index of the value to be accessed. Following is an example to understand the concept −

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Index: ${NAME[0]}"
echo "Second Index: ${NAME[1]}"

以上示例将生成以下结果 –

The above example will generate the following result −

$./test.sh
First Index: Zara
Second Index: Qadir

你可以使用以下其中一种方式访问数组中的所有项 –

You can access all the items in an array in one of the following ways −

${array_name[*]}
${array_name[@]}

此处 array_name 是您感兴趣的数组的名称。以下示例将帮助您理解该概念 -

Here array_name is the name of the array you are interested in. Following example will help you understand the concept −

#!/bin/sh

NAME[0]="Zara"
NAME[1]="Qadir"
NAME[2]="Mahnaz"
NAME[3]="Ayan"
NAME[4]="Daisy"
echo "First Method: ${NAME[*]}"
echo "Second Method: ${NAME[@]}"

以上示例将生成以下结果 –

The above example will generate the following result −

$./test.sh
First Method: Zara Qadir Mahnaz Ayan Daisy
Second Method: Zara Qadir Mahnaz Ayan Daisy