A new version of Bash supports one-dimensional arrays. Array elements can be initialized using the notation variable[xx]. Alternatively, the script can use the DECLARE-A variable statement to specify an array, and so on. To refer to an array element (that is, the value), you can use curly braces, Access form ${variable[xx]}, of course, the following are some of the old boys often used methods and a little understanding of the array, if there is advice, welcome guidance, thank Bo friends first.
Common definition methods for 1.1 shell arrays:
1) Method One:
Command law:
Dir= ($ (ls.))
Example 1: Manual command-line Operation demo
[Root@oldboy scripts]# dir= ($ (ls.))
[Root@oldboy scripts]# ls.
Oldboy.log apachemon.sh Httpdctl
[Root@oldboy scripts]# echo ${#dir [*]} <== View the length of the array
3
Command line loop to print a group element:
Writing 1:
for ((i=0; i< ' echo ${#dir [*]} '; i++))
Todo
Echo-e "${dir[$i]}\n"
Done
Tip:i< ' echo ${#dir [*]} ' can be replaced with a simpler i<${#dir [*]}, (Thanks to the eternal Night Brothers).
====================================
Writing 2:
for ((i=0; i<${#dir [*]}; i++))
Todo
Echo-e "${dir[$i]}\n"
Done
====================================
Writing 3:
For ((i=0;i<${#dir [@]};i++))
Todo
echo ${dir[${i}]}
Done
Example 2: Define and output array elements by script:
[Root@oldboy scripts]# Cat printarray.sh
Dir= ($ (ls.))
for ((i=0; i<${#dir [*]}; i++))
Todo
Echo-e "${dir[$i]}\n"
Done
[Root@oldboy scripts]# SH printarray.sh
Oldboy.log
apachemon.sh
Httpdctl
printarray.sh
====================================================
2) Method Two: Enumerating Element method
array= (red green blue Yellow magenta)
Array= (
Oldboy
Zhangyue
Zhangyang
)
Example 3: Scripting examples of enumerating element methods
[Root@oldboy ~# Cat test.sh
Array= (
Oldboy
Zhangyue
Zhangyang
)
for ((i=0; i< ${#array [*]}; i++))
Todo
echo "${array[$i]}"
Done
Echo----------------------
echo "Array len:${#array [*]}"
[Root@oldboy ~# SH test.sh
Oldboy
Zhangyue
Zhangyang
Array Len:3
3 Method 3: In fact, method three and one of the same, because of good actual combat value so separate list of explanations
Judge= ($ (curl-i-S ${url_list[$i]}|head-1|tr "\ r"))
Example 4: A more professional production check URL address script (Shell array method):
[Root@oldboy ~]# Cat check_url.sh
#!/bin/bash
# This script are created by Oldboy.
# e_mail:31333741@qq.com
# 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 seconds to perform the operation. '
For ((i=0;i<3;i++))
Todo
Echo-n "."; Sleep 1
Done
www.bianceng.cn
Echo
}
function Check_url ()
{
Wait
echo ' Check URL ... '
for ((i=0; i<${#url_list [*]}; i++))
Todo
# http/1.1 OK
Judge= ($ (curl-i-S ${url_list[$i]}|head-1|tr "\ r"))
if [[[${judge[1]} ' = = ' && ' ${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
After 3 seconds, perform the operation ....
Check URL ...
http://etiantian.org [OK]
http://www.linuxpeixun.com [OK]
http://oldboy.blog.51cto.com [OK]
Hint: The above results are colored.