Arrays in Linux

Unix / Linux - Using Shell Arrays
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.
在本章中,我们将讨论如何在Unix中使用shell数组。shell变量能够容纳单个值。这些变量称为标量变量。
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.
为Shell变量讨论的所有命名规则在命名数组时都是适用的。
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 fruits as a set of variables. Each of the individual variables is a scalar variable as follows −
假设您试图将各种水果的名称表示为一组变量。每一个单独的变量都是一个标量变量,如下所示
NAME[0]=“Apple”
NAME[1]=“Banana”
NAME[2]=“Mango”
NAME[3]=“Kiwi”
NAME[4]=“Orange”
We can use a single array to store all the above mentioned fruits name. Following is the simplest method of creating an array variable. This helps assign a value to one of its indices.
我们可以使用一个单独的数组来存储上面提到的所有水果名称。下面是创建数组变量的最简单方法。这有助于将一个值赋给它的一个索引。
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.
array_name[index]=value
这里的值array_name是数组的名称,index是要设置的数组中项的索引,value是要为该项设置的值。
As an example, the following commands −
例如,下面的命令-
NAME[0]=“Apple”
NAME[1]=“Banana”
NAME[2]=“Mango”
NAME[3]=“Kiwi”
NAME[4]=“Orange”
If you are using the bash shell, here is the syntax of array initialization −
如果您使用的是bash shell,以下是数组初始化的语法
array_name=(value1 … valuen)
Accessing Array Values
访问数组值
After you have set any array variable, you access it as follows −
设置任何数组变量之后,按如下方式访问它
${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 −
这里array_name是数组的名称,index是要访问的值的索引。下面是一个理解概念的例子
#!/bin/sh
NAME[0]=“Apple”
NAME[1]=“Banana”
NAME[2]=“Mango”
NAME[3]=“Kiwi”
NAME[4]=“Orange”
echo “First Index: ${NAME[0]}”
echo “Second Index: ${NAME[1]}”
The above example will generate the following result −
上面的例子将产生下面的结果
$./test.sh
First Index: Apple
Second Index: Banana
You can access all the items in an array in one of the following ways −
您可以通过以下方法之一访问数组中的所有项
${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 −
这里,array_name是您感兴趣的数组的名称。下面的例子将帮助你理解这个概念
#!/bin/sh

NAME[0]=“Apple”
NAME[1]=“Banana”
NAME[2]=“Mango”
NAME[3]=“Kiwi”
NAME[4]=“Orange”
echo “First Method: ${NAME[*]}”
echo “Second Method: ${NAME[@]}” The above example will generate the following result −
$./test.sh
First Method: Apple Banana Mango Kiwi Orange Second Method: Apple Banana Mango Kiwi Orange

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值