Skip to content
KARINKARIN
博客主页
项目总览
知识要点
捣鼓折腾
Git笔记
Excel修炼秘籍
github icon
  • 博客主页
    • 项目总览
          • Shell脚本笔记
            • Shell脚本介绍
              • Shell语法
                • Shell格式化输出
                  • Shell格式化输入
                    • Shell变量
                      • Shell数组
                        • 数组介绍
                          • 基本数组
                            • 数组语法
                              • 数组读出
                                • 数组赋值
                                  • 查看数组
                                    • 访问数组元素
                                      • 遍历数组
                                      • 关联数组
                                        • 定义关联数组
                                          • 关联数组赋值
                                          • 案例分享---学员信息系统
                                          • Shell运算
                                            • Shell流程控制---if判断语句
                                              • Shell流程控制---for循环语句
                                                • Shell流程控制---while循环语句
                                                  • Shell流程控制---until循环语句
                                                    • Shell流程控制---case分支语句
                                                      • Shell函数
                                                        • Shell正则表达式
                                                          • Shell对文本的搜索---grep命令
                                                            • Shell对文件的操作---sed命令
                                                              • Shell对输出流的处理---awk命令
                                                                • Shell监控脚本
                                                                  • Shell脚本---lamp环境安装

                                                                Shell数组

                                                                author iconkarincalendar icon2021年4月9日category icon
                                                                • 教程
                                                                tag icon
                                                                • shell
                                                                timer icon大约 3 分钟

                                                                此页内容
                                                                • 数组介绍
                                                                • 基本数组
                                                                  • 数组语法
                                                                  • 数组读出
                                                                  • 数组赋值
                                                                  • 查看数组
                                                                  • 访问数组元素
                                                                  • 遍历数组
                                                                • 关联数组
                                                                  • 定义关联数组
                                                                  • 关联数组赋值
                                                                • 案例分享---学员信息系统

                                                                # 数组介绍

                                                                一个变量只能存一个值,但是现实中又有很多值需要存储,那么变量就有些拘谨了。比如做一个学员信息表,一个班 50 个人,每个人 6 条信息,我们需要定义 300 个变量才能完成。恐怖恐怖,这只是一个班的学生,如果一个学校呢?一个市呢?......我想静静了!

                                                                # 基本数组

                                                                数组可以让用户一次赋予多个值,需要读取数据时只需要通过索引调用就可以方便读出了。

                                                                # 数组语法

                                                                数组名称=(元素1 元素2 元素3 ...)
                                                                
                                                                1

                                                                # 数组读出

                                                                ${数组名称[索引]}
                                                                
                                                                1

                                                                索引是元素在数组中的排队编号,默认索引是从 0 开始

                                                                # 数组赋值

                                                                方法一:一次赋一个值

                                                                # array0[0]='tom'
                                                                # array0[1]='jarry'
                                                                # array0[2]='natasha'
                                                                
                                                                1
                                                                2
                                                                3

                                                                方法二:一次赋多个值

                                                                # array1=(tom jaky alice)
                                                                # array2=(`ls ~`)
                                                                # array3=(`cat /etc/passwd`)
                                                                
                                                                1
                                                                2
                                                                3

                                                                # 查看数组

                                                                [root@localhost ~]# declare -a
                                                                
                                                                1

                                                                例如:

                                                                [root@localhost ~]# declare -a
                                                                declare -a array0=([0]="tom" [1]="jarry" [2]="natasha")
                                                                declare -a array1=([0]="pear" [1]="apple" [2]="orange" [3]="peach")
                                                                
                                                                1
                                                                2
                                                                3

                                                                # 访问数组元素

                                                                # echo ${array1[0]}					访问数组中的第一个元素
                                                                # echo ${array1[*]}					访问数组中所有有元素
                                                                # echo ${array1[@]}					访问数组中所有有元素
                                                                # echo ${#array1[@]}				统计数组元素的个数
                                                                # echo ${!array1[@]}				获取数组元素的索引
                                                                # echo ${array1[@]:1}				从数组下标1开始到最后一个元素
                                                                # echo ${array1[@]:1:3}				从数组下标1开始,访问3个元素
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7

                                                                # 遍历数组

                                                                方法一:通过数组元素进行逐个遍历

                                                                [root@localhost ~]# echo ${array1[0]}
                                                                peay
                                                                [root@localhost ~]# echo ${array1[1]}
                                                                apple
                                                                [root@localhost ~]# echo ${array1[2]}
                                                                orange
                                                                [root@localhost ~]# echo ${array1[3]}
                                                                peach
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8

                                                                方法二:通过数组元素的索引进行循环遍历

                                                                #!/bin/bash
                                                                fruits=([0]="pear" [1]="apple" [2]="orange" [3]="peach")
                                                                i=0
                                                                for ((i=0;i<${#fruits[@]};i++));do
                                                                echo ${fruits[$i]}
                                                                done
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6

                                                                # 关联数组

                                                                关联数组可以允许用户自定义数组的索引,这样使用起来更加方便、高效.

                                                                # 定义关联数组

                                                                声明关联数组变量

                                                                # declare -a ass_array1
                                                                # declare -a ass_array2
                                                                
                                                                1
                                                                2

                                                                # 关联数组赋值

                                                                方法一:一次赋一个值

                                                                # ass_array1[index1]=pear
                                                                # ass_array1[index2]=apple
                                                                # ass_array1[index3]=orange
                                                                # ass_array1[index4]=peach
                                                                
                                                                1
                                                                2
                                                                3
                                                                4

                                                                方法二:一次赋多个值

                                                                # ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]='bash shell')
                                                                
                                                                1
                                                                • 查看数组
                                                                # declare -a
                                                                ass_array1=([index1]=pear [index2]=apple [index3]=orange [index4]=beach)
                                                                ass_array2=([index1]=tom [index2]=jack [index3]=alice [index4]='bash shell')
                                                                
                                                                1
                                                                2
                                                                3
                                                                • 访问数组元素
                                                                # echo ${ass_array2[index2]}			访问数组中的第二个元素
                                                                # echo ${ass_array2[@]}					访问数组中所有元素
                                                                # echo ${#ass_array2[@]}				获取数组元素的个数
                                                                
                                                                1
                                                                2
                                                                3
                                                                • 遍历数组

                                                                针对关联数组可以通过数组元素的索引进行遍历

                                                                [root@localhost ~]# echo ${ass_array2[index1]}
                                                                tom
                                                                [root@localhost ~]# echo ${ass_array2[index2]}
                                                                jack
                                                                [root@localhost ~]# echo ${ass_array2[index3]}
                                                                alice
                                                                [root@localhost ~]# echo ${ass_array2[index4]}
                                                                bash shell
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8

                                                                # 案例分享---学员信息系统

                                                                #!/bin/bash
                                                                for ((i=0;i<3;i++))
                                                                do
                                                                read -p "输入第$((i + 1))个人名:" name[$i]
                                                                read -p "输入第$((i + 1))个年龄:" age[$i]
                                                                read -p "输入第$((i + 1))个性别:" name[$i]
                                                                done
                                                                clear
                                                                echo -e "\t\t\t\t学员查询系统"
                                                                while :
                                                                do
                                                                cp=0
                                                                echo -e "\t\t\t\t学员查询系统"
                                                                read -p "输入要查询的姓名:" xm
                                                                [ $xm == "Q" ]&&exit
                                                                for ((i=0;i<3;i++));do
                                                                if [ "$xm" == "${name[$i]}" ];then
                                                                echo "${name[$i]} ${age[$i]} ${gender[$i]}"
                                                                cp=1
                                                                fi
                                                                done
                                                                [ $cp -eq 0 ]&&echo "not found student"
                                                                done
                                                                
                                                                1
                                                                2
                                                                3
                                                                4
                                                                5
                                                                6
                                                                7
                                                                8
                                                                9
                                                                10
                                                                11
                                                                12
                                                                13
                                                                14
                                                                15
                                                                16
                                                                17
                                                                18
                                                                19
                                                                20
                                                                21
                                                                22
                                                                23
                                                                edit icon编辑此页open in new window
                                                                上一页
                                                                Shell变量
                                                                下一页
                                                                Shell运算
                                                                MIT Licensed
                                                                Copyright © 2022 karin