shell脚本编程学习之路-shell数组
1.數(shù)組的介紹
在開發(fā)shell腳本時,定義變量采用的形式為“a=1;b=2;c=3",變量多了再一個一個定義就比較麻煩,并且要是有多個不確定的變量內(nèi)容也會難以進行變量控制,于是為了解決上面的問題數(shù)組誕生了。
數(shù)組就是有限個元素變量或數(shù)據(jù)用一個名字命名,然后用編號區(qū)分他們的變量的集合,這個名字稱為數(shù)組,編號稱為數(shù)組的下標。組成數(shù)組的多個變量稱為數(shù)組的分量,也稱為數(shù)組的元素,有時也稱為下標變量。簡單的說數(shù)組就是數(shù)據(jù)類型的元素按一定順序排列的集合。
2.數(shù)組的定義與增刪改查
2.1 數(shù)組的定義
方法1:用小括號將變量值括起來賦值給數(shù)組變量,每個變量值之間要用空格進行分隔。
array=(value1 value2 value3….)示例如下:
[root@shellbiancheng ~]# array=(1 2 3) [root@shellbiancheng ~]# echo ${array[*]} 1 2 3方法2:動態(tài)地定義數(shù)組變量,并使用命令的輸出結(jié)果作為數(shù)組的內(nèi)容。
語法為:
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]} done2.2 數(shù)組的打印及輸出
(1) 打印數(shù)組元素
語法:echo ${數(shù)組名[下標]}
[root@shellbiancheng ~]# array=(one two three) [root@shellbiancheng ~]# echo ${array[0]} one 打印單個數(shù)組元素,當未指定數(shù)組下標時,數(shù)組的下標從0開始 [root@shellbiancheng ~]# echo ${array[1]} two [root@shellbiancheng ~]# echo ${array[2]} three [root@shellbiancheng ~]# echo ${array[*]} 使用*或者@符號可以得到整個數(shù)組的內(nèi)容。 one two three [root@shellbiancheng ~]# echo ${array[@]} one two three(2) 打印數(shù)組元素的個數(shù)即獲取數(shù)組的長度
語法:echo ${#array[]}
[root@shellbiancheng ~]# echo ${array[*]} one two three [root@shellbiancheng ~]# echo ${#array[*]} 3 [root@shellbiancheng ~]# echo ${#array[@]} 3(3)數(shù)組賦值(了解)
語法:數(shù)組名[下標]=內(nèi)容
如果下標不存在,則自動添加一個新的數(shù)組元素,如果存在則覆蓋原來的值。
[root@shellbiancheng jiaobenlianxi]# array=(1 2 3) [root@shellbiancheng jiaobenlianxi]# echo ${array[*]} 1 2 3 [root@shellbiancheng jiaobenlianxi]# array[3]=4 增加下標為3的數(shù)組 [root@shellbiancheng jiaobenlianxi]# echo ${array[*]} 1 2 3 4 [root@shellbiancheng ~]# array[1]=two 修改數(shù)組元素 [root@shellbiancheng ~]# echo ${array[*]} 1 two 3 4(4) 刪除數(shù)組(了解)
語法:unset 數(shù)組名[下標]
清除相應(yīng)的數(shù)組元素,如果不帶下標,則表示清除整個數(shù)組的所有值。
[root@shellbiancheng ~]# array=(1 2 3 4) [root@shellbiancheng ~]# unset array[1] 刪除下標為1的數(shù)組 [root@shellbiancheng ~]# echo ${array[*]} 1 3 4 [root@shellbiancheng ~]# unset array 刪除整個數(shù)組 [root@shellbiancheng ~]# echo ${array[*]}[root@shellbiancheng ~]#(5) 數(shù)組內(nèi)容的截取和替換(了解)
截取:
[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} 把數(shù)組中的3替換成4臨時替換,原數(shù)組未做改變 1 2 4 4 5 [root@shellbiancheng jiaobenlianxi]# array1=(${array[*]/3/4}) [root@shellbiancheng jiaobenlianxi]# echo ${array1[*]} 1 2 4 4 52.3 數(shù)組腳本開發(fā)實踐
(1)范例1:使用循環(huán)批量輸出數(shù)組元素
方法一:使用for循環(huán)語句打印數(shù)組元素
[root@shellbiancheng jiaobenlianxi]# cat array01.sh #!/bin/bash array=( 1 2 3 4 ) for ip in ${array[*]} doecho $ipsleep 2 done方法二:使用c語言型的for循環(huán)打印數(shù)組元素
[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循環(huán)語句打印數(shù)組元素
[root@shellbiancheng jiaobenlianxi]# cat array03.sh #!/bin/bash array=( 1 2 3 4 ) while((i<${#array[*]})) do echo ${array[i]} ((i++)) done3.Shell數(shù)組知識小結(jié)
(1)定義:
靜態(tài)數(shù)組:array=(1 2 3) 空格隔開
動態(tài)數(shù)組:array=($(ls))
給數(shù)組賦值:array[3]=4
(2)打印
打印所有元素:${array[*]}或${array[@]}
打印數(shù)組長度:${#array[@]}或${#array[*]}
打印單個元素:${array[i]} i是數(shù)組下標
(3) 循環(huán)打印的常用基本語法
[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轉(zhuǎn)載于:https://blog.51cto.com/10642812/2103603
總結(jié)
以上是生活随笔為你收集整理的shell脚本编程学习之路-shell数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用ABAP(ADBC)和Java(JD
- 下一篇: truncate table