歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> Linux下shell編程中數組的常見用法及示例

Linux下shell編程中數組的常見用法及示例

日期:2017/3/3 16:39:17   编辑:關於Linux

新版本的Bash支持一維數組. 數組元素可以使用符號variable[xx]等方式來初始化. 另外, 腳本可以使用declare -a variable語句來指定一個數組等。要引用一個數組元素(也就是取值), 可以使用大括號, 訪問形式為${variable[xx]},當然了,下面是一些老男孩經常使用的方法和對數組的一點理解,如有高見,歡迎指導,先謝謝博友們了。

1.1 shell數組的常用定義方法:

1)方法一:

命令法:

dir=($(ls .))

范例1:手工命令行操作演示

[root@oldboy scripts]# dir=($(ls .))

[root@oldboy scripts]# ls .

oldboy.log apachemon.sh httpdctl

[root@oldboy scripts]# echo ${#dir[*]} <==查看數組的長度

3

命令行循環打印數組元素:

寫法1:

for ((i=0; i<`echo ${#dir[*]}`; i++))

do

echo -e "${dir[$i]}\n"

done

提示:i<`echo ${#dir[*]}`可以用更簡單的寫法i<${#dir[*]}替換,(感謝永夜兄弟)。

====================================

寫法2:

for ((i=0; i<${#dir[*]}; i++))

do

echo -e "${dir[$i]}\n"

done

====================================

寫法3:

for((i=0;i<${#dir[@]};i++))

do

echo ${dir[${i}]}

done

范例2:通過腳本定義及輸出數組元素:

[root@oldboy scripts]# cat printarray.sh

dir=($(ls .))

for ((i=0; i<${#dir[*]}; i++))

do

echo -e "${dir[$i]}\n"

done

[root@oldboy scripts]# sh printarray.sh

oldboy.log

apachemon.sh

httpdctl

printarray.sh

====================================================

2)方法二:列舉元素法

array=(red green blue yellow magenta)

array=(

oldboy

zhangyue

zhangyang

)

范例3:列舉元素法的腳本例子

[root@oldboy ~# cat test.sh

array=(

oldboy

zhangyue

zhangyang

)

for ((i=0; i< ${#array[*]}; i++))

do

echo "${array[$i]}"

done

echo ----------------------

echo "array len:${#array[*]}"

[root@oldboy ~# sh test.sh

oldboy

zhangyue

zhangyang

array len:3

3)方法3:其實方法三和方法一一樣,因具有很好的實戰價值因此單獨列出講解

judge=($(curl -I -s ${url_list[$i]}|head -1|tr "\r" "\n"))

范例4:比較專業的生產檢查URL地址的腳本(shell數組方法):

[root@oldboy ~]# cat check_url.sh

#!/bin/bash

# this script is created by oldboy.

# e_mail:[email protected]

# qqinfo:49000448

# function:check web url

# version:1.1

. /etc/init.d/functions

url_list=(

http://etiantian.org

http://www.linuxpeixun.com

http://oldboy.blog.51cto.com

)

function wait()

{

echo -n '3秒後,執行該操作.';

for ((i=0;i<3;i++))

do

echo -n ".";sleep 1

done

// www.bianceng.cn

echo

}

function check_url()

{

wait

echo 'check url...'

for ((i=0; i<${#url_list[*]}; i++))

do

# HTTP/1.1 200 OK

judge=($(curl -I -s ${url_list[$i]}|head -1|tr "\r" "\n"))

if [[ "${judge[1]}" == '200' && "${judge[2]}"=='OK' ]]

then

action "${url_list[$i]}" /bin/true

else

action "${url_list[$i]}" /bin/false

fi

done

}

check_url

[root@oldboy ~]# sh check_url.sh

3秒後,執行該操作....

check url...

http://etiantian.org [ OK ]

http://www.linuxpeixun.com [ OK ]

http://oldboy.blog.51cto.com [ OK ]

提示:上述結果是帶顏色的。

范例5:實現lvs負載均衡健康檢查及自動剔除和自動加入RS的腳本(多年前的腳本)

[root@oldboy sbin]# cat health_check_url.sh      
#!/bin/bash
# this script is created by oldboy.
# e_mail:[email protected]
# qqinfo:31333741
# function:
# version:1.1
PORT="80"
VIP=192.168.1.181
RIP=(
192.168.1.178
192.168.1.179
)
function check_url()
{
for ((i=0; i< ${#RIP[*]}; i++))
do
judge=($(curl -I -s http://${RIP[$i]}|head -1|tr "\r" "\n"))
if [[ "${judge[1]}" == '200' && "${judge[2]}"=='OK' ]]
then
if [ `ipvsadm -L -n|grep "${RIP[$i]}"|wc -l` -ne 1 ]
then
ipvsadm -a -t $VIP:$PORT -r ${RIP[$i]}:$PORT
fi

else
if [ `ipvsadm -L -n|grep "${RIP[$i]}"|wc -l` -eq 1 ]
then
ipvsadm -d -t $VIP:$PORT -r ${RIP[$i]}:$PORT
fi
fi
done
}

while true
do
check_url
sleep 5
done

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

1.2 閱讀博文後可以練習的小例子

問題1: 把1-3 3個數字存到數組裡 分別乘以8 然後依次輸出。

解答:

下面的評論裡已有正確答案,有興趣的朋友可以先思考寫寫,然後看評論,如果有問題歡迎評論提問,如果力所能及老男孩願意為你解答疑惑。

問題2:一個SHELL腳本:oldboy.sh內容為定義一個數組array=(1 2 3)

要打印數組元素的個數。

要求:要把數組array當作變量在執行腳本時傳參到腳本裡使用(這塊是本題的難點)

例如:這樣執行 sh oldboy.sh array

解答:

下面的評論裡已有正確答案,有興趣的朋友可以先思考寫寫,然後看評論,如果有問題歡迎評論提問,如果力所能及老男孩願意為你解答疑惑。

更多數組的知識,大家可以參考:

http://www.etiantian.org/ebooks/cn_shell_abs/html/arrays.html

本文出自 “老男孩的linux博客” 博客,請務必保留此出處http://oldboy.blog.51cto.com/2561410/1055734

Copyright © Linux教程網 All Rights Reserved