脚本进阶数组
1.声明数组
1)普通数组可以不支持声明,直接使用
declare -a ARRAT_NAME
2)关联数组必须先声明,再使用
declare -A ARRAY_NAME
2.数组赋值
1)一次赋值一个
ARRAY_NAME[INDEX]=VALUE
例:
[root@centos7 ~]# weilan[0]=kai [root@centos7 ~]# weilan[2]=kimmich
2)一次赋值全部
ARRAY_NAME=("VAL1""VAL2".....)
例:
[root@centos7 ~]# title=("joshua""kimmich""sule")
[root@centos7 ~]# num=({0..10})
[root@centos7 ~]# alpha=({a..g})
[root@centos7 ~]# file=(*.sh)
3)只赋值特定元素
ARRAY_NAME=([0]="VAL1" [3]="VAL2"...)
4)交互式数值赋值
[root@centos7 ~]# read -a kkk 111
3.显示数组
declare -a

4.引用数组
1)使用某一个数组中的元素
${ARRAY_NAME[INDEX]}
注意:若不写索引则默认为0
2)引用数组所有元素
${ARRAY_NAME[*]}
${ARRAY_NAME[@]}
3)显示数组的长度即元素个数
${#ARRAY_NAME[*]}
${#ARRAY_NAME[@]}
5.删除数组
1)删除数组中的某一个元素,会导致稀疏格式
unset ARRAY[INDEX]
2)删除整个数组
unset ARRAY
6.范例:数组的基本使用
1)声明一个数组
[root@centos7 ~]# declare -a title
2)查看该数组
[root@centos7 ~]# declare -a | grep 'title'

3)赋值(一次一个)
[root@centos7 ~]# title[0]=ceo; title[1]=coo; title[2]=cto;title[3]=ufo
4)显示数组的所有元素
[root@centos7 ~]# echo ${title[*]}
ceo coo cto ufo
[root@centos7 ~]# echo ${title[@]}
ceo coo cto ufo
5)显示数组长度
[root@centos7 ~]# echo ${#title[*]}
4
[root@centos7 ~]# echo ${#title[@]}
4
6)显示数组中某一个元素
[root@centos7 ~]# echo ${title[0]}
ceo
[root@centos7 ~]# echo ${title[3]}
ufo
7)创建并赋值一个数组
[root@centos7 ~]# car=(bmw benz audi xiaomi)
[root@centos7 ~]# echo ${#car[*]}
4
[root@centos7 ~]# echo ${car[*]}
bmw benz audi xiaomi
[root@centos7 ~]# echo ${car[2]}
audi
8)创建一个以本目录的所有元素以.sh文件名的数组
[root@centos7 data]# file=(*.sh)
[root@centos7 data]# echo ${file[@]}
f10.sh f1.sh f2.sh f3.sh f4.sh f5.sh f6.sh f7.sh f8.sh f9.sh
9)对其交互式变量赋值
[root@centos7 data]# read -a name
11 22 33 44 55 66 77
[root@centos7 data]# echo ${name[1]}
22
[root@centos7 data]# echo ${#name[*]}
7
7.关联数组问题
赋值定义一个关联数组
1)
[root@centos7 data]# read -a name
11 22 33 44 55 66 77
[root@centos7 data]# echo ${name[1]}
22
[root@centos7 data]# echo ${#name[*]}
7
我们显示其元素:
[root@centos7 data]# echo ${family[num]}
3
[root@centos7 data]# echo ${family[address]}
3
[root@centos7 data]# echo ${family[name]}
3
发现出问题了,结果都是3,其实是因为我们要对关联数组先声明再定义,所以思路清晰
2)将其删掉,重新来过
[root@centos7 data]# unset family [root@centos7 data]# declare -a | grep 'family' [root@centos7 data]#
3)先声明再定义
[root@centos7 data]# declare -A family
[root@centos7 data]# family[name]=weilan; family[address]=beijing; family[num]=3
[root@centos7 data]# echo ${#family[*]}
3
[root@centos7 data]# echo ${family[@]}
weilan 3 beijing
[root@centos7 data]# echo ${family[name]}
weilan
8.数组数据处理
1)数组切片:
格式:
${ARRAY[@]:uffset:number}
例:跳过前三个取后续四个
[root@centos7 data]# n=({1..10})
[root@centos7 data]# echo ${n[@]}
1 2 3 4 5 6 7 8 9 10
[root@centos7 data]# echo ${n[*]:3:4}
4 5 6 7
2)数组后续增加一个元素(拿当前元素个数当新元素的下标),因为最后一个元素的下标+1=数组元素个数,所以将元素个数作为索引来新增元素:
[root@centos7 data]# n[${#n[*]}]=11
[root@centos7 data]# echo ${#n[@]}
11
[root@centos7 data]# echo ${n[@]}
1 2 3 4 5 6 7 8 9 10 11
注意:这种方式并不适用于稀松格式
3)稀松格式增加新元素
例:创建稀疏环境
[root@centos7 data]# unset n[3]
[root@centos7 data]# echo ${n[*]}
1 2 3 5 6 7 8 9 10 11
自己指定所添加的元素位置。
9.范例:
1)生成10个随机数保存于数组中,并找出其最大值和最小值
[root@centos7 dir1]# vim shuzu1
#!/bin/bash
declare -i max min
declare -a nums
for ((i=0;i<10;i++));do
nums[$i]=$RANDOM
[ $i -eq 0 ] && min=${nums[0]} && max=${nums[0]} && continue
[ ${nums[$i]} -gt $max ] && max=${nums[$i]} && continue
[ ${nums[$i]} -lt $min ] && min=${nums[$i]}
done
echo "All numbers are ${nums[*]}"
echo Max is $max
echo Min is $min
[root@centos7 dir1]# bash shuzu1
All numbers are 3665 1833 6615 6535 1277 12191 3472 28622 29023 16843
Max is 29023
Min is 1277

2)冒泡排序(shell有点折磨就暂时用c替代吧)
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int arr[7] = { 6, 1, 1, 2, 8, 3, 7 };
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7 - i; j++)
{
if (arr[j] < arr[j + 1])
{
int Temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = Temp;
}
}
}
for (int i = 0; i < 7; i++)
{
printf("%d ", arr[i]);
}
system("pause>0");
return 0;
}
3)快速排序
#include <stdio.h>
#include <stdlib.h>
void Sort(int* arr, int left, int right)
{
if (left >= right)
return;
else
{
int q = left;
int h = right;
while (q < h)
{
while (q < h && arr[h] >= arr[left])
{
h--;
}
while (q < h && arr[q] <= arr[right])
{
q++;
}
if (q == h)
break;
int Temp = arr[q];
arr[q] = arr[h];
arr[h] = Temp;
}
if (q != left)//guiwei
{
int temp = arr[q];
arr[q] = arr[left];
arr[left] = temp;
}
//递归
Sort(arr, left, q - 1);
Sort(arr, h + 1, right);
}
}
int main(void)
{
int arr[10] = { 0, 5, 3, 6, 2, 1, 9, 7, 4, 8 };
Sort(arr, 0, 9);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
system("pause>0");
return 0;
}

4)插入排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[7] = { 7, 10, 8, 9, 15, 5};
for (int i = 1; i < 7; i++)
{
int Temp = a[i];
for (int j = i - 1; j >= 0; j--)
{
if (Temp <= a[j])
{
a[j + 1] = Temp;
break;
}
a[j + 1] = a[j];
if (j == 0)
{
a[0] = Temp;
}
}
}
for (int i = 0; i < 7; i++)
{
printf("%d ", a[i]);
}
system("pause>0");
return 0;
}

5)选择排序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int a[10] = { 8, 6, 10, 1, 4, 7, 9, 2, 5, 3 };
int rig = 9;
for (int j = 1; j <= 9; j++)
{
int min = a[0];
int iNdex = 0;
for (int i = 1; i <= rig; i++)
{
if (min > a[i])
{
min = a[i];
iNdex = i;
}
}
int Temp = a[rig];
a[rig] = a[iNdex];
a[iNdex] = Temp;
(rig--);
}
for (int i = 0; i <= 9; i++)
{
printf("%d ", a[i]);
}
system("pause>0");
return 0;
}

浙公网安备 33010602011771号