当前位置:网站首页>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.
边栏推荐
- Memo - about C # generating barcode for goods
- 华为游戏初始化init失败,返回错误码907135000
- 搭建一个通用监控告警平台,架构上需要有哪些设计
- R language epidisplay package ordinal or. The display function obtains the summary statistical information of the ordered logistic regression model (the odds ratio and its confidence interval correspo
- 透过华为军团看科技之变(六):智慧公路
- 助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
- ETL development of data warehouse (IV)
- Leetcode-128 longest continuous sequence
- LiveData postValue会“丢”数据
- Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
猜你喜欢
搭建一个通用监控告警平台,架构上需要有哪些设计
网易游戏,激进出海
小红书上的爱情买卖
Lumiprobe lumizol RNA extraction reagent solution
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Altair HyperWorks 2022 software installation package and installation tutorial
linux下清理系统缓存并释放内存
Leetcode203 remove linked list elements
研究了11种实时聊天软件,我发现都具备这些功能…
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
随机推荐
How to realize the bottom layer of read-write lock in go question bank 16
前4A高管搞代运营,拿下一个IPO
2020,最新手机号码手机验证正则表达式,持续更新
解决方案:可以ping别人,但是别人不能ping我
AppGallery Connect场景化开发实战—图片存储分享
Taiaisu M source code construction, peak store app premium consignment source code sharing
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
数据库基础:select基本查询语句
华为游戏初始化init失败,返回错误码907135000
PriorityQueue的用法和底层实现原理
R语言ggplot2可视化:gganimate包transition_time函数创建动态散点图动画(gif)、shadow_wake函数配置动画的渐变效果(gradual falloff)拖尾效应
GameFramework食用指南
如何运营好技术相关的自媒体?
How to operate technology related we media well?
app发版后的缓存问题
Golang error handling
毕业总结
R language uses the aggregate function of epidisplay package to divide numerical variables into different subsets based on factor variables, and calculate the summary statistics of each subset
js找出数字在数组中下一个相邻的元素
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference