当前位置:网站首页>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.
边栏推荐
- Huawei cloud experts explain the new features of gaussdb (for MySQL)
- Mipi interface, DVP interface and CSI interface of camera [easy to understand]
- 数据仓库(四)之ETL开发
- Halcon image calibration enables subsequent image processing to become the same as the template image
- VBA simple macro programming of Excel
- Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
- Usage and underlying implementation principle of PriorityQueue
- 网易游戏,激进出海
- Implement a Prometheus exporter
- Li Kou daily question - Day 32 -1232 Dotted line
猜你喜欢

Three. JS learning - basic operation of camera (learn from)

Huawei game failed to initialize init with error code 907135000

LeetCode-21合并两个有序链表

After studying 11 kinds of real-time chat software, I found that they all have these functions

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

Leetcode-21 combines two ordered linked lists

Write an open source, convenient and fast database document query and generation tool with WPF

Implement a Prometheus exporter

如何使用物联网低代码平台进行个人设置?

Evaluation of 6 red, yellow and black list cameras: who is the safest? Who has good picture quality? From now on, let you no longer step on thunder
随机推荐
隐私沙盒终于要来了
华为游戏初始化init失败,返回错误码907135000
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Qfile read / write file operation in QT
GameFramework食用指南
How does factor analysis calculate weights?
ES6 summary "suggestions collection" of array methods find(), findindex()
Privacy sandbox is finally coming
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Prices of Apple products rose across the board in Japan, with iphone13 up 19%
Solution: you can ping others, but others can't ping me
Leetcode-160 intersecting linked list
Taiaisu M source code construction, peak store app premium consignment source code sharing
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
力扣每日一题-第32天-1232. 缀点成线
Viewing the whole ecology of Tiktok from a macro perspective
Go Technology Daily (2022-02-14) - go language slice interview real questions 8 consecutive questions
Leetcode-21 combines two ordered linked lists
用GSConv+Slim Neck改进Yolov5,将性能提升到极致!