1. Introduction to Arrays
Usually define a=1;b=2;c=3, the variables are many, and then a definition of a laborious.
Simply put, an array is a collection of elements of the data type arranged in a certain order.
An array is a finite element variable or data that is named after a name, and then a collection of their variables, which is called an array, and the number is called an array subscript. The multiple variables that make up an array are called the components of an array, also known as elements of an array, sometimes called subscript variables.
2. Definition of arrays and the search for additional deletions
(1) Definition of an array
A pair of parentheses represents an array, and the array elements are separated by a "space" symbol
array= (value1 value2 value3 ...)
[[email protected] ~]# array=(1 2 3)
(2) Get the length of the array
[[email protected] ~]# echo ${#array[*]}
3
[[email protected] ~]# echo ${#array[@]}
3
(3) Printing array elements
[[email protected] ~]# echo ${array[0]}
1
[[email protected] ~]# echo ${array[1]}
2
[[email protected] ~]# echo ${array[2]}
3
[[email protected] ~]# echo ${array[*]}
1 2 3
(4) for loop print set
Method One:
[[email protected] jiaobenlianxi]# cat array01.sh
array=(
192.168.1.111
192.168.1.112
192.168.1.113
)
for ip in ${array[*]}
do
echo $ip
sleep 2
done
[[email protected] jiaobenlianxi]# sh array01.sh
192.168.1.111
192.168.1.112
192.168.1.113
Method Two: In C language mode
[[email protected] jiaobenlianxi]# cat array02.sh
array=(
192.168.1.114
192.168.1.115
192.168.1.116
)
for((i=0;i<${#array[*]};i++))
do
echo ${array[i]}
sleep 2
done
[[email protected] jiaobenlianxi]# sh array02.sh
192.168.1.114
192.168.1.115
192.168.1.116
(5) Increase of array
[[email protected] jiaobenlianxi]# array=(1 2 3)
[[email protected] jiaobenlianxi]# echo ${array[*]}
1 2 3
[[email protected] jiaobenlianxi]# array[3]=4
[[email protected] jiaobenlianxi]# echo ${array[*]}
1 2 3 4
(6) Deleting an array
[[email protected] jiaobenlianxi]# unset array[0]
[[email protected] jiaobenlianxi]# echo ${array[*]}
2 3 4
(7) Interception and substitution of array contents (much like the substitution of variable quantum strings)
Intercept:
[[email protected] jiaobenlianxi]# array=(1 2 3 4 5)
[[email protected] jiaobenlianxi]# echo ${array[*]:1:3}
2 3 4
[[email protected] jiaobenlianxi]# echo ${array[*]:3:4}
4 5
Replace:
[[email protected] jiaobenlianxi] # echo $ {array [*] / 3/4} Replace 3 in the array with 4 temporarily, the original array is unchanged.
1 2 4 4 5
[[email protected] jiaobenlianxi] # array1 = ($ {array [*] / 3/4})
[[email protected] jiaobenlianxi] # echo $ {array1 [*]}
1 2 4 4 5
3. Command result definition array
[[email protected] jiaobenlianxi]# array=($(ls))
[[email protected] jiaobenlianxi]# echo ${array[1]}
1.sh
[[email protected] jiaobenlianxi]# echo ${array[2]}
a.log
[[email protected] jiaobenlianxi]# echo ${array[3]}
array01.sh
[[email protected] 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
[[email protected] jiaobenlianxi]# cat array03.sh
array=(
$(ls)
)
for((i=0;i<${#array[*]};i++))
do
echo ${array[i]}
done
4.Shell Array Knowledge Summary
(1) Definition:
Static array: array= (1 2 3) space separated
Dynamic array: array= ($ (LS))
Assigning a value to an array: array[3]=4
(2) Printing
Print all elements: ${array[*]} or ${array[@]}
Print array Length: ${#array [@]} or ${#array [*]}
Print a single element: ${array[i]} i is an array subscript
Shell Script Programming Learning Note-shell Array