当前位置:网站首页>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.
边栏推荐
- Qt中的QFile读写文件操作
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- 斯坦福、Salesforce|MaskViT:蒙面视觉预训练用于视频预测
- Leetcode-141 circular linked list
- 搭建一個通用監控告警平臺,架構上需要有哪些設計
- 力扣每日一题-第32天-1232. 缀点成线
- Why do independent website sellers start to do social media marketing? The original customer conversion rate can be improved so much!
- Lumiprobe 双功能交联剂丨Sulfo-Cyanine5 双-NHS 酯
- Regular expression
- golang 错误处理
猜你喜欢
Lumiprobe 生物分子定量丨QuDye 蛋白定量试剂盒
研究了11种实时聊天软件,我发现都具备这些功能…
What designs are needed in the architecture to build a general monitoring and alarm platform
力扣每日一题-第32天-589.N×树的前序遍历
Facebook聊单,SaleSmartly有妙招!
市值蒸发740亿,这位大佬转身杀入预制菜
透过华为军团看科技之变(六):智慧公路
Memo - about C # generating barcode for goods
Privacy sandbox is finally coming
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
随机推荐
Popular science: what does it mean to enter the kernel state?
LeetCode-21合并两个有序链表
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
docker 部署mysql8.0
Viewing technological changes through Huawei Corps (VI): smart highway
Three simple methods of ES6 array de duplication
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
解决方案:可以ping别人,但是别人不能ping我
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
毕业总结
Technology implementation and Architecture Practice
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
OpenAI|视频预训练 (VPT):基于观看未标记的在线视频的行动学习
Go language self-study series | go language data type
linux下清理系统缓存并释放内存
如何使用物联网低代码平台进行个人设置?
Summary of cases of players' disconnection and reconnection in Huawei online battle service
app发版后的缓存问题
摄像头的MIPI接口、DVP接口和CSI接口[通俗易懂]
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享