shell指令碼快速入門之 陣列

2021-10-08 13:02:17 字數 4766 閱讀 3151

四、陣列的操作

五、陣列指令碼例項

什麼是陣列?陣列是存放相同型別資料的集合,在記憶體中開闢了連續的空間,通常配合迴圈使用

普通陣列:不需要宣告直接定義,下標索引只能是整數

關聯陣列:需要用declare -a宣告否則系統不識別,索引可以是字串

如:arr=(11 22 33 44)

陣列名稱:arr

陣列元素:11,12,12,14,陣列元素預設是以空格分隔的

陣列長度:4,有4個元素

陣列下標:每個陣列元素都有乙個下標索引,一般從0開始,33是陣列第三個元素,但是它的下標索引是2

第一種:直接把要加入陣列的元素用小括號括起來,中間用空格分開

num=(11 22 33 44)

第二種:精確的給每乙個下標索引定義乙個值加入陣列,索引數字可以不連續

num=([0]=55 [1]=66 [2]=77 [4]=88)

第三種:先把要加入陣列的元素全部先賦值給乙個變數,然後引用這個變數加入到陣列

list=「11 12 13 14」

num=($list)

第四種:可以把命令的結果用小括號括起來新增到陣列,那麼陣列會以空格或者製表符區分每乙個元素

num=(cat /etc/passwd)

[

root@server myscripts]#

arr=(10

2030

4050)/

/定義了乙個arr陣列

[root@server myscripts]#

echo$/

/檢視陣列中索引是

3的元素40[

root@server myscripts]#

echo$/

/檢視陣列中所有的元素

1020

304050[

root@server myscripts]#

echo$/

/檢視陣列中所有的元素,同$

1020

3040

50

[

root@server myscripts]#

declare

-adeclare

-agroups

='()'

declare

-apipestatus

='([0]="0")'

declare

-aarr

='([0]="10" [1]="20" [2]="30" [3]="40" [4]="50")'

declare

-anum

='([0]="55" [1]="66" [2]="77" [4]="88")'

[

root@server myscripts]#

echo$0

1241

2

[

root@server myscripts]#

echo$4

12

[

root@server myscripts]#

echo$/

/只是臨時替換

5566

5588

[root@server myscripts]#

echo$55

6677

88

1)刪除陣列元素

[root@server myscripts]#

unset

num[2]

//unset後面跟陣列元素的下標

[root@server myscripts]#

[root@server myscripts]#

echo$55

6688

[root@server myscripts]#

echo$3

[root@server myscripts]#

echo$0

13(2

)刪除整個陣列

[root@server myscripts]#

unset

num/

/後面直接跟陣列名即可

[root@server myscripts]#

declare

-adeclare

-agroups

='()'

declare

-apipestatus

='([0]="0")'

declare

-aarr

='([0]="10" [1]="20" [2]="30" [3]="40" [4]="50")'

#!/bin/bash

list=`

seq1

100`

num=

($list

)echo$[

root@server myscripts]#

vimarr3.sh

#!/bin

/bash

for((i

=0;i

<=99

;i++

));do

arr[$i]

=$[$i

+1]done

echo$[

root@server myscripts]#

.arr3.sh

1234

5678

9101112

1314

1516

1718

1920

2122

2324

2526

2728

2930

3132

3334

3536

3738

3940

4142

4344

4546

4748

4950

5152

5354

5556

5758

5960

6162

6364

6566

6768

6970

7172

7374

7576

7778

7980

8182

8384

8586

8788

8990

9192

9394

9596

9798

99100

#!/bin/bashj=

0for((

i=1;

i<

100;i+

+))do

if[$[

$i%2]

-ne0]#

if[`expr$i%

2`-ne0

]這樣也可以

then

num[$j]

=$ifiletj+

+done

echo

$

[

root@server ~]#

vimpaixu2.sh

#!/bin

/bash

arr=(90

204050)

max=

$for((

i=0;

i<$;

i++)

)doif[

$max -lt $

]then

max=$fi

done

echo

$max

[root@server ~]#

.paixu2.sh

90

#!/bin/bash

arr()"

}while:do

read

-p"您想要加入元素嗎?[y/n]: "

addif

["$add"==

"y"]

then

arrelif

["$add"==

"n"]

then

echo

"再見!"

break

fidone

#!/bin/bash

num=(90

9090

5090

405030)

for((i

=1;i

<=$

;i++

))doif[$

[num

[$i-1]

]-lt60]

then

num[

$i-1]=

60fi

done

echo

$

[

root@server ~]#

vimunset.sh

#!/bin

/bash

arr=(70

6080

403020)

i=0forvin

$;doif[$v

-lt60];

then

unset

arr[$i]

fileti++

done

echo$[

root@server ~]#

.unset.sh

7060

80

Shell指令碼快速入門 1

shell 是乙個用 c 語言編寫的程式,使用者可以通過shell指令碼語言來進行程式開發。與其他指令碼語言不同,shell指令碼所需的直譯器一般是內建在作業系統的,而像node.js php等指令碼語言需要手動安裝直譯器程式才可以。接下來將和大家一起來學習shell指令碼程式設計。shell直譯器...

shell指令碼 之入門

who 可以檢視現在系統有誰登入。who wc l 計算使用者的個數 cat filename 建立檔案,使用cat複製終端的內容到filename 檔案中 context 在終端中輸入的內容 d ctrl d表示end of file cat filename 檢視filename檔案中的內容,將...

Shell指令碼之陣列

陣列是存放相同型別資料的集合,陣列中可以存放多個值,經常配合迴圈語句進行使用。例如 有乙個陣列 num 15 16 17 18 19 陣列名稱 num 陣列元素 第1個 11 陣列長度 5 陣列下標 元素18所對應的下標為3shell 陣列用括號來表示,元素用 空格 符號分割開,語法格式如下 方法一...