当前位置:网站首页>5、 Array base
5、 Array base
2022-06-24 01:39:00 【jackxiao】
Shell An array of is an array of finite elements ( Variable or character content ) Give a name to , A collection of elements that are then numbered to distinguish them . This name is called the array name , The number used to distinguish different contents is called array subscript . The elements that make up the array ( Variable ) Elements called arrays , Sometimes called subscript variable An array is also a variable variable , Therefore, the array of related functions of variable substring is also applicable , By default, array subscripts are from 0 Start counting .
Array related knowledge through man bash Then search for Arrays To get to know .
1. Define an array
1) Statically named arrays
- Method 1[ recommend ]
array1=(1 2 3) - Method 2
array2=( www.baidu.com www.sina.com )
- Method 3
array=([0]=one [1]=two [2]=three [3]=four)
2) Dynamic commands create arrays
array=($(ls)) array=(`ls`)
2. Print array
1) Print all elements :
$(array[@]) $(array[*])
2) Print array length :
$(#array[@]) $(#array[*])
3) Print a single element :
$(array[i])
3. Add or modify array data
1) Add to change
array[0]=one array[1]=two array[2]=three
If the corresponding subscript value does not exist , New assignment , If it already exists , The original value will be overwritten
4. Delete array data
Because arrays are essentially variables , So you can go through “unset Array [ Subscript ]” Clear the corresponding array elements , Without subscript , Means to clear all data of the entire array .
unset array[1] unset array
5. Slice and replace [ Variable substring ]
Arrays are also variables , Therefore, it is also suitable for the functional application of substring processing of variables explained above .
1) Slicing function
array=(1 2 3 4 5)
echo ${array[@]:1:3}2 3 4 From the subscript for 1 Start intercepting the elements of , Take together 3 Array elements . For other usage, refer to the usage of variable substring
2) Replace
array=(1 2 3 1 1)
echo ${array[@]/1/b}b 2 3 b b Put... In the array 1 Replace with b, The original array has not been modified , and sed It's like . For other usage, refer to the usage of variable substring The calling method is :
${ Array name [@ or *]/ To find the character / Replace character }This operation does not change the original array
Two 、 Case study
1. Number of elements in the content Print array
array=(1 2 3 4 4) && echo ${array[@]}1 2 3 4 4echo ${#array[@]}5echo ${array[1]}2
By default, array subscripts are from 0 Start counting
2. for Loop print array
Use arrays and for The cycle will “i am oldboy my teacher is luogang” In this sentence , The number of characters is less than or equal to 6 Print out words of
cat /server/scripts/d1.sh
#!/bin/bash
array=(i am oldboy my teacher is luogang)
for n in ${array[*]}
do
if [ `echo $n|wc -L` -le 6 ]
then
echo $n
fi
done
echo -----------------
for((i=0;i<${#array[*]};i++))
do
if [ ${#array[i]} -le 6 ]
then
echo ${array[i]}
fi
done- sh /server/scripts/d1.sh
边栏推荐
- CDN access log quality performance monitoring and operation statistical analysis best practices
- Introduction to easycvr interfacing with Huawei IVS subscription camera and user change request interface
- Logistics industry supplier collaborative management platform supplier life cycle management to optimize logistics costs
- Oracle sqlldr quick import and sqluldr2 quick export
- How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
- Flink weapon: introduction to the open source platform streamx
- Horizontal movement of Intranet penetration -- password spray attack from domain to domain
- How do users of Fortress computers add servers? How much does it cost to add servers for fortress users?
- Talk about 11 tips for interface performance optimization
- Typescript type system
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)

【Flutter】如何使用Flutter包和插件
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter
![[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)](/img/c8/f6c2a62b8ab8fa88bd2b3d8f35f592.jpg)
[SQL injection 12] user agent injection foundation and Practice (based on burpsuite tool and sqli labs LESS18 target machine platform)
![[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)](/img/b5/a8c4bbaf868dd20b7dc9449d2a4378.jpg)
[SQL injection 13] referer injection foundation and Practice (based on burpseuite tool and sqli labs less19 target platform)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
随机推荐
Gin framework: implementing timeout Middleware
How to implement NSQ delay streaming technology in easycvr?
How to write the domain name of trademark registration? What is the process of trademark and domain name registration?
Horizontal movement of Intranet penetration -- password spray attack from domain to domain
Batch generation of 2D codes from txt files
SAP mm Migo 411k error - correct the customizing settings for the unique
Common e-commerce data index system
Glusterfs version 4.1 selection and deployment
Activity Registration - Tencent cloud digital intelligence driven SME transformation and upgrading series salon · Zhuhai station
Build a smart drug supervision platform based on easycvr video technology and build a drug safety firewall
What is the cost of domain name trademark registration? What is the use of domain names and trademarks?
"Cross border" layout photovoltaic circle Green Island wind invested 4.6 million to layout distributed photovoltaic power generation
How to use voice synthesis? Can voice synthesis modify the voice?
I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
Virtual currency mining detection and defense
How to set up AI speech synthesis? What is the function of speech synthesis?
What you don't know about traifik
The Mars rescue plan has been released. The year of the tiger is limited to dolls waiting for you!
Property management source code based on wechat applet
Is the domain name available for trademark registration? How long is the trademark registration cycle?