Linux Bash阵列和元素

Creating a variable is easy and very useful to make simple applications. If the variable count goes up managing variables becomes a pain. Think that if the variables count is higher than 100 how easily can be created, assigned. There is a simpler usage for multiple variables. It is called an array. An array is a variable that holds more than one value with similar types. For example, we have a script that should connect to a lot of hosts. We can use array in a single variable to hold all of the hostnames.

创建变量很容易,并且对于制作简单的应用程序非常有用。 如果变量数量增加,管理变量就很麻烦。 想一想,如果变量计数大于100,那么创建和分配变量的难易程度。 多个变量的用法更简单。 它称为数组。 数组是一个变量,其中包含多个具有相似类型的值。 例如,我们有一个应该连接到许多主机的脚本。 我们可以在单个变量中使用数组来保存所有主机名。

创建数组inBash (Creating Array inBash)

Creating array is easy as variable.

创建数组很容易作为变量。

$ hosts[0]="www1"
$ hosts[1]="www2"
$ hosts[2]="db"
$ hosts[3]="file"

As you see array is a variable that holds a series of data. Series are generally held in a sequential manner. There are alternative and more practical ways to define arrays. To access individual elements in an array index numbers are used. [0]is an index number that addresses the first element of the array.

如您所见,数组是一个包含一系列数据的变量。 系列通常以顺序方式进行。 有其他更实用的方法来定义数组。 为了访问数组中的各个元素,使用索引号。 [0]是一个索引编号,用于寻址数组的第一个元素。

$ hosts=(www1 www2 db file)

The above script will create the same array with the previous script. Spaces will be used as delimiters and 4 array elements will be added into an array with the index starting from 0. If you want to shuffle array index numbers and there is alternative syntax too. Look below

上面的脚本将与先前的脚本创建相同的数组。 空格将用作定界符,并且会将4个数组元素添加到索引从0开始的数组中。如果要改组数组索引号,还可以使用其他语法。 往下看

$ hosts=([2]=www1 [1]=www2 [3]=db [0]=file)

Here we create array by specifying index numbers. We set www1 element index number to 2 .

在这里,我们通过指定索引号来创建数组。 我们将www1元素索引号设置为2。

访问数组数据 (Accessing Array Data)

Arrays are accessed regularly after defined. Array access is similar to variable access but there is little difference.

定义后定期访问数组。 数组访问与变量访问相似,但差别不大。

$ echo ${hosts[0]}

Here we access hosts arrays first element. We put an array variable and index into curly braces with dollar sign. The difference from the normal variable is we add curly braces between the dollar sign and variable name.

在这里,我们访问主机数组的第一个元素。 我们将一个数组变量和索引插入带有美元符号的花括号中。 与普通变量的区别在于我们在美元符号和变量名称之间添加了花括号。

LEARN MORE  Linux Bash exit and Exit Codes
了解更多Linux Bash退出和退出代码

获取数组长度(Getting Array Length)

Arrays have multiple elements. The element count maybe 5 or 5000. Sometimes we cannot specify array element count because it is generated dynamically. So we need to know the element count.

数组具有多个元素。 元素数可能为5或5000。有时我们无法指定数组元素数,因为它是动态生成的。 因此,我们需要知道元素计数。

$ echo ${#hosts[@]}
$ 4

The # and @ operators are used to get element count of an array.

@运算符用于获取数组的元素计数。

删除数组元素 (Deleting Array Elements)

As we see before array elements can be created in different ways. Sometimes we may need to delete specific array elements from an array. Deleting an array element is different from deleting array completely. Only specified elements are deleted and other elements stay with the same index. After deleting and element the index becomes empty.

正如我们之前看到的,可以用不同的方式创建数组元素。 有时我们可能需要从数组中删除特定的数组元素。 删除数组元素不同于完全删除数组。 仅删除指定的元素,其他元素保留相同的索引。 删除和元素后,索引变为空。

$ unset ${names[2]}       
$ echo ${names[2]}  
$

翻译自: https://www.poftut.com/linux-bash-arrays-elements/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值