当前位置:网站首页>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
边栏推荐
- Use Navicat software to connect self built database (Linux system)
- How about speech synthesis? Is speech synthesis effective?
- Code practice | ground extraction with logo-loam
- "Cross border" layout photovoltaic circle Green Island wind invested 4.6 million to layout distributed photovoltaic power generation
- How to learn website construction does website construction need code
- Application analysis of video edge computing gateway easynvr in video overall monitoring solution
- Many ministries and commissions strengthened regulation, and Tencent security helped enterprises resist the "mining" Trojan horse
- [technical grass planting] the cloud driver takes you straight to the clouds
- I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000
- How is the national standard easygbs video technology applied in the comprehensive supervision scenario of the power supply business hall?
猜你喜欢

It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
![[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)

I, a 27 year old female programmer, feel that life is meaningless, not counting the accumulation fund deposit of 430000

【Flutter】如何使用Flutter包和插件
![[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)
![[flutter] comment utiliser les paquets et plug - ins flutter](/img/a6/e494dcdb2d3830b6d6c24d0ee05af2.png)
[flutter] comment utiliser les paquets et plug - ins flutter
随机推荐
How to choose a website construction company self-study website or website construction company
How to implement NSQ delay streaming technology in easycvr?
It's too difficult for me. Ali has had 7 rounds of interviews (5 years of experience and won the offer of P7 post)
Textfree - reverse engineering of textfree
Build a smart pole visual intercom system through an intelligent gateway
"Cross border" layout photovoltaic circle Green Island wind invested 4.6 million to layout distributed photovoltaic power generation
Introduction to trusted service manager
Can the fortress machine connect to the ECS? What are the reasons why the fortress cannot connect to the ECS?
Interviewer: why does the new generation memory need two survivor zones?
Location and troubleshooting of memory leakage: analysis of heap profiling principle
Dart series: generics in dart classes
Software cost evaluation: a method for estimating software scale by fast function point method
A marriage app_ T signature analysis
Cloud computing "keeping the promise"
How does the fixed assets management software reduce costs and increase efficiency for enterprises?
Use Navicat software to connect self built database (Linux system)
How is the national standard easygbs video technology applied in the comprehensive supervision scenario of the power supply business hall?
How do small businesses do a good job in website construction? Is there any guarantee for network companies to build websites
How to build a high-quality website
Remove the cloud disk service display "continued" logo