日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

shell脚本编程学习之路-shell数组

發布時間:2025/7/25 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 shell脚本编程学习之路-shell数组 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.數組的介紹

在開發shell腳本時,定義變量采用的形式為“a=1;b=2;c=3",變量多了再一個一個定義就比較麻煩,并且要是有多個不確定的變量內容也會難以進行變量控制,于是為了解決上面的問題數組誕生了。

數組就是有限個元素變量或數據用一個名字命名,然后用編號區分他們的變量的集合,這個名字稱為數組,編號稱為數組的下標。組成數組的多個變量稱為數組的分量,也稱為數組的元素,有時也稱為下標變量。簡單的說數組就是數據類型的元素按一定順序排列的集合。

2.數組的定義與增刪改查

2.1 數組的定義

方法1:用小括號將變量值括起來賦值給數組變量,每個變量值之間要用空格進行分隔。

array=(value1 value2 value3….)

示例如下:

[root@shellbiancheng ~]# array=(1 2 3) [root@shellbiancheng ~]# echo ${array[*]} 1 2 3

方法2:動態地定義數組變量,并使用命令的輸出結果作為數組的內容。

語法為:

array=($())或者array=(`命令`)

示例如下:

[root@shellbiancheng jiaobenlianxi]# array=($(ls)) [root@shellbiancheng jiaobenlianxi]# echo ${array[1]}1.sh [root@shellbiancheng jiaobenlianxi]# echo ${array[2]} a.log [root@shellbiancheng jiaobenlianxi]# echo ${array[3]}array01.sh [root@shellbiancheng jiaobenlianxi]# echo ${array[*]} 1-100.sh 1.sh a.log array01.sh array02.sh beijingcolor.sh b.log break.sh case01.sh check.sh chkconfigoff.sh chkconfigon.sh color1.sh color.sh for1-1000.sh for1-100_3.sh for1-100.sh for1.sh for2.sh for3.sh for4.sh for5.sh hanshu01.sh hanshu02.sh huafei.sh menufruit.sh nginx.sh piliangxiugai1.sh piliangxiugai2.sh piliangxiugai3.sh suijiwnjian.sh sum1-100.sh test uptime.sh usage.sh user.sh while1.sh while2.sh while3.sh while4.sh while.sh zhuajiu.sh [root@shellbiancheng jiaobenlianxi]# cat array03.sh array=( $(ls) )for((i=0;i<${#array[*]};i++))do echo ${array[i]} done

2.2 數組的打印及輸出

(1) 打印數組元素

語法:echo ${數組名[下標]}

[root@shellbiancheng ~]# array=(one two three) [root@shellbiancheng ~]# echo ${array[0]} one 打印單個數組元素,當未指定數組下標時,數組的下標從0開始 [root@shellbiancheng ~]# echo ${array[1]} two [root@shellbiancheng ~]# echo ${array[2]} three [root@shellbiancheng ~]# echo ${array[*]} 使用*或者@符號可以得到整個數組的內容。 one two three [root@shellbiancheng ~]# echo ${array[@]} one two three

(2) 打印數組元素的個數即獲取數組的長度

語法:echo ${#array[]}

[root@shellbiancheng ~]# echo ${array[*]} one two three [root@shellbiancheng ~]# echo ${#array[*]} 3 [root@shellbiancheng ~]# echo ${#array[@]} 3

(3)數組賦值(了解)

語法:數組名[下標]=內容

如果下標不存在,則自動添加一個新的數組元素,如果存在則覆蓋原來的值。

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3) [root@shellbiancheng jiaobenlianxi]# echo ${array[*]} 1 2 3 [root@shellbiancheng jiaobenlianxi]# array[3]=4 增加下標為3的數組 [root@shellbiancheng jiaobenlianxi]# echo ${array[*]} 1 2 3 4 [root@shellbiancheng ~]# array[1]=two 修改數組元素 [root@shellbiancheng ~]# echo ${array[*]} 1 two 3 4

(4) 刪除數組(了解)

語法:unset 數組名[下標]

清除相應的數組元素,如果不帶下標,則表示清除整個數組的所有值。

[root@shellbiancheng ~]# array=(1 2 3 4) [root@shellbiancheng ~]# unset array[1] 刪除下標為1的數組 [root@shellbiancheng ~]# echo ${array[*]} 1 3 4 [root@shellbiancheng ~]# unset array 刪除整個數組 [root@shellbiancheng ~]# echo ${array[*]}[root@shellbiancheng ~]#

(5) 數組內容的截取和替換(了解)

截取:

[root@shellbiancheng jiaobenlianxi]# array=(1 2 3 4 5) [root@shellbiancheng jiaobenlianxi]# echo ${array[*]:1:3} 2 3 4 [root@shellbiancheng jiaobenlianxi]# echo ${array[*]:3:4} 4 5

替換:

[root@shellbiancheng jiaobenlianxi]# echo ${array[*]/3/4} 把數組中的3替換成4臨時替換,原數組未做改變 1 2 4 4 5 [root@shellbiancheng jiaobenlianxi]# array1=(${array[*]/3/4}) [root@shellbiancheng jiaobenlianxi]# echo ${array1[*]} 1 2 4 4 5

2.3 數組腳本開發實踐

(1)范例1:使用循環批量輸出數組元素

方法一:使用for循環語句打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array01.sh #!/bin/bash array=( 1 2 3 4 ) for ip in ${array[*]} doecho $ipsleep 2 done

方法二:使用c語言型的for循環打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array02.sh #!/bin/bash array=( 1 2 3 4 ) for((i=0;i<${#array[*]};i++)) doecho ${array[i]}sleep 2 done

方法三:使用while循環語句打印數組元素

[root@shellbiancheng jiaobenlianxi]# cat array03.sh #!/bin/bash array=( 1 2 3 4 ) while((i<${#array[*]})) do echo ${array[i]} ((i++)) done

3.Shell數組知識小結

(1)定義:

靜態數組:array=(1 2 3) 空格隔開

動態數組:array=($(ls))

給數組賦值:array[3]=4

(2)打印

打印所有元素:${array[*]}或${array[@]}

打印數組長度:${#array[@]}或${#array[*]}

打印單個元素:${array[i]} i是數組下標

(3) 循環打印的常用基本語法

[root@shellbiancheng jiaobenlianxi]# cat array02.sh #!/bin/bash array=( 1 2 3 4 ) for((i=0;i<${#array[*]};i++)) do echo ${array[i]} sleep 2 done

轉載于:https://blog.51cto.com/10642812/2103603

總結

以上是生活随笔為你收集整理的shell脚本编程学习之路-shell数组的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。