1. 程式人生 > >shell 數組介紹

shell 數組介紹

數組賦值 集合 org log cep 批量 control shouji 下標

  平時的定義a=1;b=2;c=3,變量如果多了,再一個一個定義很繁瑣,並且取變量值也很累。簡單的說,數組就是各種數據類型的元素按一定順序排列的集合。

數組就是把有限個元素變量或數組用一個名字命名,然後用編號區分他們的變量的集合。這個名字成為數組名,編號成為數組下標。組成數組的各個變量成為數組的分量,也稱為數組的元素,有時也稱為下標變量。

如果有過用其它語言編程的經歷,那麽想必會熟悉數組的概念。由於有了數組,可以用相同的名字引用一系列變量,並用數字(索引)來識別它們。在許多場合,使用數組可以縮短和簡化程序開發,因為可以利用索引值設計一個循環,高效處理多種情況。

數組定義與增刪改查:

數組定義:array=(1 2 3)

獲取數組的長度:echo ${#array[@]} 或echo ${#array[*]}

打印數組元素:echo ${array[0]} 或echo ${array[1]}

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

技術分享圖片

測試:用數組定義ip地址,然後用for循環打印出來

vim array.sh
array=(
10.1.1.1
10.1.1.2
10.1.1.3
)
for ip in ${array[*]}
do
    echo $ip
    sleep 1
done
C語言型:
array=(
10.1.1.1
10.1.1.2
10.1.1.3
) for ((i=0;i<${array[@]};i++)) do echo ${array[]i} done

技術分享圖片

數組賦值:

[[email protected] scripts]# array=(1 2 3)
[[email protected] scripts]# echo ${array[*]}
1 2 3
[[email protected] scripts]# array[3]=4    #增加數組元素
[[email protected] scripts]# echo ${array[*]}
1 2 3 4
[[email protected] scripts]# array[0]=bqh     #修改數組元素
[[email protected] scripts]# echo ${array[*]}
bqh 2 3 4

技術分享圖片

數組刪除:直接用unset數組[下標]可以清楚相應的元素,不帶下標,清楚整個數組

[[email protected] scripts]# echo ${array[*]}
bqh 2 3 4
[[email protected] scripts]# unset array[0]
[[email protected] scripts]# echo ${array[*]}
2 3 4
[[email protected] scripts]# unset array
[[email protected] scripts]# echo ${array[*]}

[[email protected] scripts]#

技術分享圖片

數組內容的截取和替換:

截取:

[[email protected] scripts]# array=(1 2 3 4 5)
[[email protected] scripts]# echo ${array[*]:1:3}
2 3 4
[[email protected] scripts]# echo ${array[*]:3:2}
4 5

調用方法:${數組名[@或*]:起始位置:長度}

替換:

[[email protected] scripts]# echo ${array[*]/5/6}   #把數組中的5替換成6,臨時生效,和sed很像
1 2 3 4 6
[[email protected] scripts]# echo ${array[*]}
1 2 3 4 5

技術分享圖片

調用方法:${數組名[*或@]/查找字符/替換字符}

-----------------------------------------------------------------------

測試:把系統命令結果作為數組元素,然後一一打印出來

[[email protected] scripts]# array=($(ls))    #或array=($`ls`)
[[email protected] scripts]# echo ${array[0]}
array.sh
[[email protected] scripts]# echo ${array[1]}
bqh.sh
[[email protected] scripts]# echo ${array[*]}
array.sh bqh.sh for.sh nginx nginx1 nginx.sh shoujicz.sh sjyjcx.sh while1_100sum.sh while_rz.sh while.sh while_sjcz.sh
[[email protected] scripts]#

技術分享圖片

[[email protected] scripts]# vim array1.sh 
#!/bin/sh
array=(ls cd pwd chmod charry echo mkdir awk sed grep) for i in ${array[*]} do echo $i done echo "===========================" array1=(ls cd pwd chmod charry echo mkdir awk sed grep)
for ((i=0;i<${#array1[*]};i++)) do echo ${array1[i]} done

技術分享圖片

OK,我們來一個實戰測試:

批量檢查網站狀態(數組for循環方法)

首先拿一個一個網站檢查,沒問題了在用數組定義批量檢測。

我們常用curl、nmap、ping等方式檢測,下為curl方法:

[[email protected] scripts]# curl -I www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: Keep-Alive
Content-Length: 277
Content-Type: text/html
Date: Wed, 15 May 2019 15:21:10 GMT
Etag: "575e1f6f-115"
Last-Modified: Mon, 13 Jun 2016 02:50:23 GMT
Pragma: no-cache
Server: bfe/1.0.8.18

[[email protected] scripts]# curl -I www.baidu.com 2>/devnull|egrep "200|302"
HTTP/1.1 200 OK
[[email protected] scripts]# curl -I www.baidu.com 2>/devnull|egrep "200|302"|wc -l
1
[[email protected] scripts]# curl -I www.taobao.com 2>/devnull|egrep "200|302"
HTTP/1.1 302 Found
[[email protected] scripts]# curl -I www.taobao.com 2>/devnull|egrep "200|302"|wc -l
1
[[email protected] scripts]# 

技術分享圖片

我們只需要檢測狀態信息返回值有200或302時就代表網站暢通,反之異常。

單個測試沒問題,我們接著寫腳本:

[[email protected] scripts]# vim curl.sh 
#!/bin/sh
# ******************************************************
# Author       : aゞ錦衣衛 
# Last modified: 2019-05-15 23:35
# Email        : [email protected]
# blog         : https://www.cnblogs.com/su-root
# Filename     : curl.sh
# Description  :curl jiance
# ******************************************************
array=(
www.baidu.com
www.kanq.com.cn
www.bqh.com
www.jyw.org
www.taobao.com
www.jd.com
)
for n in ${array[*]}
do
curl=`curl -I -m 3 $n 2>/dev/null|egrep "200|302"|wc -l`
  if [ $curl -eq 1 ];then
    echo -e "$n \033[32m is ok!\033[0m"
  else
    echo -e "$n \033[33;5m is not ok!\033[0m"
  fi
done

技術分享圖片

當然還有其它很多方法,這裏只介紹了curl。

其它方法:

ping ip地址/域名 #等於0

nmap ip地址 -p 端口|grep open|wc -l #等於1

wget --spider --timeout=10 --tries=2 ip地址 &>/dev/null #返回值等於0

shell 數組介紹