Introduction to Shell arrays

Source: Internet
Author: User
Introduction to Shell arrays
Time: 2008-01-23 10:57:33 Source: Linux Federation Collection collation Author:
You can also use array variables in bash, which are assigned two types of values:

(1) name = (value1 ... valuen) at this time subscript starting from 0
(2) Name[index] = value

There is no limit to the scope of the array subscript, and there is no need to use successive components.




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


$ a= (A b c def)

==================================================
$ echo ${a[@]}//Take all elements
A b c def

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

$ echo ${a[0]}//Take first element
A

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

To get the number of array elements
$ echo ${#A [@]}
4
$ echo ${#A}
4
$ echo ${#A [3]}//Get the length of element 3
$

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

$ a[3]=yaoshuyin//re-assign the third element
$ echo ${a[@]}
A b c yaoshuyin

==================================================
Clear variables
$ unset A
$ echo ${a[@]}
$

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

Empty the variable, and the value becomes empty
$ a=
$ echo ${a[@]}
$
==================================================

A=b
B=c
Unset $A The fact that the variable being canceled is B instead of a


======================= example While loop ========================

#建立数组
Arrsource= ("arrjobs.php" "arrsubhangye.php" "arrfirst.php")

arrdest= ("BUILDHR" \
"Buildtrain/htdocs" \
"Bankhr" \
"Healthr" \
"Elehr" \
)

#取数组无元素个数
lenarrsource=${#arrSource}
lenarrdest=${#arrDest}


#循环列出数组元素
I=0
While [$i-lt $lenArrSource]
Todo
echo ${arrsource[$i]}
Let i++
Done


I=0
While [$i-lt $lenArrDest]
Todo

echo ${arrdest[$i]}
Let i++
Done


======================= Example: For loop ===============================

#源文件
Arrsource= ("/home/800hr/htdocs/login_jump.php")

#目标网站
arrDest=(ithr elehr buildhr bankhr healthr ctvhr chenhr mechr clothr cneduhr 56hr tourhr foodhr greenhr cnlawhr waimaohr)

For outer in ${arrsource} #${arrsource} is an array of all elements
Todo
For inner in ${arrdest}
Todo
echo "Ln-s $outer/home/${inner}/campus/"
Done
Done
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.