当前位置:网站首页>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.
边栏推荐
猜你喜欢

搭建一個通用監控告警平臺,架構上需要有哪些設計

Lumiprobe 生物分子定量丨QuDye 蛋白定量试剂盒

The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise

力扣每日一题-第32天-589.N×树的前序遍历

Li Kou daily question - Day 32 -589 N × Preorder traversal of tree

Basic knowledge and commands of disk

Memo - about C # generating barcode

Lumiprobe lumizol RNA extraction reagent solution

Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!

Improve yolov5 with gsconv+slim neck to maximize performance!
随机推荐
助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
Write it down once Net travel management background CPU Explosion Analysis
VBA simple macro programming of Excel
Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
R语言使用epiDisplay包的followup.plot函数可视化多个ID(病例)监测指标的纵向随访图、使用n.of.lines参数指定显示的病例数
为什么独立站卖家都开始做社交媒体营销?原来客户转化率能提高这么多!
How does factor analysis calculate weights?
Leetcode-128 longest continuous sequence
Case study on comprehensive competitiveness of principal components
Graduation summary
Qfile read / write file operation in QT
《Go题库·16》读写锁底层是怎么实现的
Create your own NFT collections and publish a Web3 application to show them (Introduction)
洞态在某互联⽹⾦融科技企业的最佳落地实践
Leetcode203 remove linked list elements
如何运营好技术相关的自媒体?
Lumiprobe biomolecular quantification - qudye Protein Quantification Kit
R language ggplot2 visualization: gganimate package transition_ Time function to create dynamic scatter animation (GIF), shadow_ The wake function configures the gradient falloff tailing effect of the
ES6数组去重的三个简单办法
PriorityQueue的用法和底层实现原理