shell 程式設計 2 陣列

2021-10-04 03:53:26 字數 1267 閱讀 3961

陣列:

只支援一維,不限定大小。

下標由0開始,可以是整數或算數表示式,大於或等於0。

1. 定義陣列

arr_name=(val0, val1, ..., valn)

arr_name=(

val00

val11

arr_name[n]=valuen

下標可以不連續。

[root@k8s-master test0]# arr_name=(val0, val1, val2)

[root@k8s-master test0]# arr_name1=(

> val00

> val11

> val22

> )

[root@k8s-master test0]# arr_name[0]=value0

[root@k8s-master test0]# arr_name[5]=value5

[root@k8s-master test0]# echo $arr_name

value0

2. 讀取陣列

格式: $

如果無{},則會輸出arr_name的0號元素加上[index]。

[root@k8s-master test0]# arr_name[0]=value0

[root@k8s-master test0]# arr_name[5]=value5

[root@k8s-master test0]# echo $arr_name

value0

[root@k8s-master test0]# echo $arr_name[5]

value0[5]

[root@k8s-master test0]# echo $

value5

[root@k8s-master test0]#

3. 陣列長度

元素個數:

len=$

len=$

單個元素長度

len_elem=$

[root@k8s-master test0]# echo $

4[root@k8s-master test0]# echo $

4[root@k8s-master test0]# echo $arr_name

value0

[root@k8s-master test0]# echo $

6[root@k8s-master test0]#

shell程式設計 陣列

1 定義陣列 通過指定元素值來定義陣列 array key value eg.array 1 one array 3 three shell中的陣列的下標 索引 從0開始 也可以通過declare語句定義陣列 declare a array 沒必要,在shell中,所有的變數都不必顯式定義就可以用作...

Shell程式設計 陣列

1.4 求陣列最大值 1.5 示例 1.6 陣列的操作 1.7 shell指令碼除錯 1.3.1 陣列包括的資料型別 陣列只可存放數值或字元 1.3.2 陣列操作第一種方法每個元素和他後面的元素挨個比 大的放後面 root shell shuzu vimshuzu paixu.sh bin bash...

shell程式設計陣列

一.陣列 陣列簡介 變數 用乙個固定的字串,代替乙個不固定字串。陣列 用乙個固定的字串,代替多個不固定字串。型別普通陣列 只能使用整數作為陣列索引 關聯陣列 可以使用字串作為陣列索引 圖示 變數切片有個索引的概念。乙個索引 整數 對應乙個字元。普通陣列 中的索引對應乙個字串。關聯陣列 陣列中的索引可...