当前位置:网站首页>Shell array
Shell array
2022-07-01 19:05:00 【Hadron's blog】
One , Array
- Method 1
Array name =(value0 value1 value2 …)
array1=(10 20 30 40 50)
- 1.
Method 2
Array name =([0]=value [1]=value [2]=value …)
array2=([0]=10 [1]=20 [2]=30 [3]=40 [4]=50)
- 1.
Method 3
List name =“value0 value1 value2 …”
Array name =($ List name )
abc="10 20 30 40 50"
array3=($abc)
- 1.
- 2.
Method four
Array name [0]=“value”
Array name [1]=“value”
Array name [2]=“value”
array4[0]=10
array4[1]=20
array4[2]=30
array4[3]=40
array4[4]=50
- 1.
- 2.
- 3.
- 4.
- 5.
Two , Data type of array
1、 value type
Character type ( character string ): Use " " or ’ ' Definition , Prevent spaces in elements , Elements are separated by spaces
2、 Get array length
echo ${# Array name [*]}
array1=(10 20 30 40 50)
echo ${#array1[*]}
- 1.
- 2.
3、 Read an index assignment
echo ${ Array name [*]}—— Read the whole array value
echo ${ Array name [x]}—— Read index x+1 The numerical
echo ${array1[*]}
echo ${array1[1]}
echo ${array1[3]}
- 1.
- 2.
- 3.
- 4.
- 5.
4、 Array traversal
#!/bin/bash
arr=(50 40 30 20 10)
for i in ${arr[@]}
do
echo $i
done
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
5、 Array slice
obtain ${ Array name [@ or *]: The starting position : length } Value
Output the entire array , here * And @ identical
arry1=(10 20 30 40 50)
echo ${arry1[*]}
echo ${arry1[*]:0:2}
echo ${arry1[*]:1:4}
echo ${arry1[*]:2:2}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
6、 Array substitution
Replace with a single value
array1[2]=88
- 1.
Multiple substitutions ( A temporary replacement )
echo ${array1[*]/0/66}
- 1.
7、 Array delete
Delete the entire array
unset Array name
unset arry
- 1.
Delete the specified location
unset Array name [ Reference no. ]
unset array3[2]
- 1.
8、 Array appends elements
Method 1
Single add
Array name [X]=Y
array3[2]=30
- 1.
Method 2 ( There must be no omission in the middle value element )
Without any deletion , Index max , It's the length of the element minus one
Array name [$#{ Array name [*]}]=X
array3[${#array3[*]}]=88
- 1.
Method 3
Directly get all the elements of the source array plus the new elements to be added , Re assign the array together , Refresh definition index
Double quotation marks cannot be omitted , Otherwise, when there are elements with spaces in the array, the elements will be divided into multiple by spaces
Can't be “@” Replace with “*”, If replaced by an asterisk , Without double quotation marks “@” In the same way , In double quotation marks , The array array_name All elements in are added to the array as one element
Array name =("${ Array name [@]}" Numeric element x Numeric element y Numeric element z)
array3=("${array3[@]}" 99 100)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
Method four
Array name +=( The number x The number y The number z)
array3+=(200 300 400)
- 1.
3、 ... and , Array parameters
#!/bin/bash
fun () {
echo " The function received a value of :[email protected]"
[email protected]
echo " The value of the new array is :${newarr[@]}"
}
arr=(60 20 30 40 50)
echo "arr The value of the array is :${arr[@]}"
fun ${arr[@]}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
Four , Sort
1. Shell Sort
Also known as reduced increment method . The basic idea is this :
1. First select a less than N The integer of gap As the first increment , Then set all the distances to gap The elements of are in the same group , And directly insert and sort the elements of each group . Then take an integer smaller than the first increment as the second increment , Repeat the above operation …
2. When the size of the increment is reduced to 1 when , It's equivalent to the whole sequence being divided into a group , Perform a direct insert sort , Sort complete .
problem : Why should we let gap From big to small ?
answer:gap The bigger it is , The faster the data moves ;gap The smaller it is , The slower the data moves . Let gap more , It can make the data move to its corresponding position faster , Reduce the number of moves .
- 1.
- 2.
- 3.
7 6 8 3 1 5 2 4
gap=(8+1)/2=4 Time comparison , Put the big one back
7-1 -> 1-7
6-5 -> 5-6
8-2 -> 2-8
3-4 -> 3-4
1 5 2 3 7 6 8 4
gap=4/2=2 Time comparison
1 2 7 8
1 2->1 2
1 2 7-->1 2 7
1 2 7 8--->1 2 7 8
5 3 6 4 Each round of comparison takes the last element of the current round and compares it with other elements in the group , Put the large array back
5 3->3 5
3 5 6-->3 5 6
3 5 6 4--->3 4 5 6
1 3 2 4 7 5 8 6
gap=2/2=1 Time comparison
1 3->1 3
1 3 2-->1 2 3
1 2 3 4--->1 2 3 4
1 2 3 4 7---->1 2 3 4 7
1 2 3 4 7 5----->1 2 3 4 5 7
1 2 3 4 5 7 8------>1 2 3 4 5 7 8
1 2 3 4 5 7 8 6------->1 2 3 4 5 6 7 8
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
2. Insertion sort
Insertion sort , Also called direct insertion sort . In the actual , When we play cards , We use the idea of insertion sort .
The basic idea :
In the element to be sorted , Before hypothesis n-1 Elements are ordered , We will now move to n Elements are inserted into the previously arranged sequence , Make the front n The elements are in order . Insert all elements in this way , Until the whole sequence is in order .
#!/bin/bash
arr=(63 4 24 1 3 15)
echo ${arr[*]}
length=${#arr[@]}
for ((i=0; i<$length; i++))
{
for((j=0; j<=$i; j++))
{
if [ ${arr[$i]} -lt ${arr[$j]} ]
then
tmp=${arr[$i]}
arr[$i]=${arr[$j]}
arr[$j]=$tmp
fi
}
}
echo ${arr[*]}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
3. Bubble sort
It's like a bubble rising , Will move the data in the array from small to large or from large to small .
The basic idea :
The basic idea of bubble sorting is to compare the values of two adjacent elements , Exchange element values if conditions are met , Move the smaller elements to the front of the array , Move large elements to the back of the array ( That is, to exchange the positions of two elements ), So the smaller elements rise from the bottom to the top like bubbles .
#!/bin/bash
read -p " Enter the array to be sorted :" x
array=($x)
echo " Old array is :${array[@]}"
# Get array length
length=${#array[@]}
for ((i=1;i<$length;i++))
do
# Determine the position of the value , Put the big one back , And the number of comparisons decreases with the increase of the number of rounds
for ((a=0;a<$length-$i;a++))
do
# Define the value of the first and second comparison
first=${array[$a]}
second=${array[$a+1]}
if [ $first -gt $second ];then
# Assign the first value to a temporary variable temp
temp=${array[$a]}
# Assign the second value to the first value
array[$a]=${array[$a+1]}
# Then put the value of the first value ( Temporary variable temp) Give the second value
array[$a+1]=$temp
fi
done
done
echo " The sorted array order is :${array[@]}"
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
边栏推荐
- 如何运营好技术相关的自媒体?
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- ETL development of data warehouse (IV)
- Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
- [quick application] win7 system cannot run and debug projects using Huawei ide
- Leetcode-21 combines two ordered linked lists
- 华为联机对战服务玩家掉线重连案例总结
- 精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
- Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
- lefse分析
猜你喜欢

实现一个Prometheus exporter

Huawei cloud experts explain the new features of gaussdb (for MySQL)

毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?

Lumiprobe lumizol RNA extraction reagent solution

用GSConv+Slim Neck改进Yolov5,将性能提升到极致!

AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award

华为云专家详解GaussDB(for MySQL)新特性

Memo - about C # generating barcode for goods

洞态在某互联⽹⾦融科技企业的最佳落地实践

小红书上的爱情买卖
随机推荐
毕业总结
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
Leetcode-83 delete duplicate elements in the sorting linked list
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
Case study on comprehensive competitiveness of principal components
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
R语言使用dplyr包的transmute函数计算dataframe数据中的指定数据列的移动窗口均值、使用ggplot2包可视化移动均值与原始数据的折线图
华为游戏初始化init失败,返回错误码907135000
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
golang 错误处理
Leetcode-160 intersecting linked list
AppGallery Connect场景化开发实战—图片存储分享
Halcon image calibration enables subsequent image processing to become the same as the template image
Usage and underlying implementation principle of PriorityQueue
Lumiprobe Lumizol RNA 提取试剂解决方案
精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment
How does factor analysis calculate weights?
Lumiprobe biomolecular quantification - qudye Protein Quantification Kit
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!