One, array 1. Definition
Variables: Storing the memory space of a single element
Array: A contiguous memory space that stores multiple elements, which is equivalent to multiple variables
Collection
Array name and Index
Index: Numbering starting from 0, which is a numeric index
#注意: Indexes can support the use of custom formats, not just numeric formats
, which is the associated index, after the bash4.0 version begins to support
The array of Bash supports sparse format (index discontinuity)
2. Common Commands and options
Declare-x varname Setting Environment variables
Declare-i declaring integers
Declare-g set function as global function
DECLARE-XF Setting Environment functions
DECLARE-A array name Declaration index array
DECLARE-A array name declaration associative array
Declare-a View all indexed arrays
Declare-a View all relational arrays
3. Array General format
Title[0]=boss
Title[1]=ceo
Title[2]=coo
Title[3]=cto
~]# name= (Liubei guanyu Zhangfei Zhaoyun)
~]# course= ([0]=linux [2]=python] [5]=oracle]
~]# read-a Menu
Huimian Lamian Yanroutang
Script= (/root/t*.sh)
Digit= ({1..20})
Example 1:
Array Assignment
~]# digit= (1 3 5)
References to arrays
~]# Echo $digit $ array name references only the first value
1
~]# echo ${digit[1]} specifies the subscript to reference the specified value
3
Reference all Values
~]# Echo ${digit[*]}
1 3 5
~]# Echo ${digit[@]}
1 3 5
Print Quantity
~]# echo ${#digit [@]}
3
Reference last Value
~]# echo ${digit[$[${#digit [@]}-1]]}
5
Example 2
Each value is displayed separately, and each value is wrapped in a line.
Such as
~]# for N in $ (seq 0 $[${#testshuzu [*]}-1]);d o
Echo testshuzu[$n] is ${testshuzu[$n]}
Done
~]# Testshuzu[0] is Liubei
TESTSHUZU[1] is Guanyu
TESTSHUZU[2] is Zhaofei
TESTSHUZU[3] is Huangzhong
TESTSHUZU[4] is Zhaoyun
4. Array Common options
Add value to the index array and add to the last
~]# testshuzu[${#testshuzu []}]=machao
~]# for n with $ (seq 0 $[${#testshuzu []}-1]);d o echo testshuzu[$n] is ${testshuzu[$n]}; Done
Testshuzu[0] is Liubei
TESTSHUZU[1] is Guanyu
TESTSHUZU[2] is Zhaofei
TESTSHUZU[3] is Huangzhong
TESTSHUZU[4] is Zhaoyun
TESTSHUZU[5] is Machao
To delete a value in an array
~]# unset testshuzu[1]
Delete entire array
~]# unset Testshuzu
Slices of an array
Offset: Number of elements to skip
Number: How many elements to remove
All elements after the offset is taken
~]# echo ${testshuzu[]:1:3}
Zhaofei Weiyan Zhaoyun
~]# echo ${testshuzu[]:1} skips a value and takes the last
Zhaofei Weiyan Zhaoyun Machao Machao
Associative arrays
Two different ways:
One: Declare first, then assign value
~]# declare-a Testshuzu
~]# testshuzu= ([Zhugong]=liubei [Erdi]=guanyu] [Sandi]=zhangfei]
Two: Declaration, assignment one step
Declare-a weiguo= ([Chengxiang]=caocao [Junshi]=simayi] [Jiangju]=xiahoudun]
Example 3
Writing a script generates an array named digit that contains 10 random numbers, displays all the values of the array, and then displays the maximum value, the minimum value.
#!/bin/bash
Declare-i min Max #声明最大值和最小值
Declare-a Digit #创建数组名
For ((i=0;i<10;i++));d o #从0开始循环, Loop 10 times
digit[$i]= $RANDOM #数组是随机数
[$i-eq 0] && min=${digit[0]} && max=${digit[0]} && continue #将第一次随机数赋给最大最小值进行下次循环
[${digit[$i]}-gt $max] && max=${digit[$i]} && continue #上一次随机数和这一次相比 take maximum value
[${digit[$i]}-lt $min] && min=${digit[$i]} && continue #上一次随机数和这一次相比 take the minimum value
Done
echo all digit is ${digit[*]} #输出所有随机数
Echo Max is $max
Echo Min is $min
Write a script that defines an array in which the elements in the array are all files ending in. Log in the/var/log directory, and the sum of the rows in the file whose subscript is an even number
#!/bin/bash
Sum=0
File= (/var/log/. Log) #创建数组
For ((i=0;i<=$[${#File []}-1];i++));d o #循环到所有数组
If [$[$i%2]-eq 0];then #判断是否为偶数文件
line=$ (Cat ${file[$i]} | wc-l) #统计偶数文件行数
sum=$[$Sum + $Line] #将每次循环的文件行数加起来
Fi
Done
Echo $Sum
Second, string processing
~]# Echo ${name}
Dema West Asia
~]# Echo ${name:2:3} #从左边跳过2个字符, take 3 characters
Ma West
~]# Echo ${name:2} #从左边跳过2个字符, take the last
MA West Asia
~]# echo ${name:-2} #从右向左取2个字符, note at least one space after the colon
Western Asia
~]# echo ${name: -3:1} #从右向左取3个字符, then 1 characters from left to right
A
~]# echo ${name:-3:-1} #从右向左取3个字符, then 1 characters from right to left
A West
~]# Echo ${name:3:-1} #从左边跳过3个字符, skip 1 characters from the right
A West
Find from left to right
~]# Echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~]# echo ${path#Local}
/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~]# echo ${path##Local} #贪婪模式
/bin:/usr/sbin:/usr/bin:/root/bin
Find from left to right
~]# Echo ${path}
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~]# echo ${path%local}
/usr/local/sbin:/usr/
~]# echo ${path%%local}
/usr/
Find replacements
~]# ${var/pattern/substr} #查找var所表示的字符串中, the first string to be matched to by pattern, in order to replace the SUBSTR
~]# ${var//pattern/substr} #查找var所表示的字符串中, all strings that can be matched by the pattern to be replaced by substr
~]# ${var/#pattern/substr} #查找var所表示的字符串中 the string to which the beginning of the line is matched by pattern, to substr replace the
~]# ${var/%pattern/substr} #查找var所表示的字符串中 the string to which the end of the line is matched by pattern, in order to substr replace the
Find and delete
~]# ${var/pattern} #删除var所表示的字符串中第一次被pattern所匹配到的字符串
~]# ${var//pattern} #删除var所表示的字符串中所有被pattern所匹配到的字符串
~]# ${var/#pattern} #删除var所表示的字符串中所有以pattern为行首所匹配到的字符串
~]# ${var/%pattern} #删除var所表示的字符串中所有以pattern为行尾所匹配到的字符串
Uppercase and lowercase conversions
~]# name= "Zhangfei Liubei"
~]# echo ${name,,}
Zhangfei Liubei
~]# Echo ${name^^}
Zhangfei Liubei
Array and string handling of Liunx