• linux 中shell数组的应用


    001、生成数组, 生成数组a

    root@ubuntu2204:/home/test# a[0]=800
    root@ubuntu2204:/home/test# a[1]=500
    root@ubuntu2204:/home/test# a[2]="kkk"
    root@ubuntu2204:/home/test# a[3]="ggg"
    root@ubuntu2204:/home/test# a[4]="xxx"
    root@ubuntu2204:/home/test# a[5]=777
    root@ubuntu2204:/home/test# a[6]=666

    002、输出数组单个元素

    root@ubuntu2204:/home/test# echo ${a[0]}
    800
    root@ubuntu2204:/home/test# echo ${a[4]}
    xxx

    003、批量输出数组元素

    root@ubuntu2204:/home/test# for i in $(seq 0 5); do echo ${a[i]}; done
    800
    500
    kkk
    ggg
    xxx
    777
    root@ubuntu2204:/home/test# for i in $(seq 0 6); do echo ${a[i]}; done
    800
    500
    kkk
    ggg
    xxx
    777
    666

    004、使用一行生成数组

    root@ubuntu2204:/home/test# declare -a b=(111 "xxx" 555 800)
    root@ubuntu2204:/home/test# echo ${b[1]}
    xxx
    root@ubuntu2204:/home/test# echo ${b[3]}
    800

    005、在1行中打印整个数组

    root@ubuntu2204:/home/test# declare -a a=(100 200 "xxx" "yyy")
    root@ubuntu2204:/home/test# echo ${a[@]}
    100 200 xxx yyy

    006、获取数组的长度

    root@ubuntu2204:/home/test# declare -a a=(100 200 300 400)
    root@ubuntu2204:/home/test# echo ${#a[@]}
    4
    root@ubuntu2204:/home/test# declare -a b=(100 200 300 400 "xxx" "yyy")
    root@ubuntu2204:/home/test# echo ${#b[@]}
    6

    007、数组中单个元素的长度

    root@ubuntu2204:/home/test# declare -a a=(100 33333 2 44)
    root@ubuntu2204:/home/test# echo ${#a[0]}
    3
    root@ubuntu2204:/home/test# echo ${#a[2]}
    1
    root@ubuntu2204:/home/test# echo ${#a[1]}
    5
    root@ubuntu2204:/home/test# echo ${#a[3]}
    2

    008、数组切片

    root@ubuntu2204:/home/test# a=(100 "xxx" 200 888 999 "yyy" "zzz")
    root@ubuntu2204:/home/test# echo ${a[@]:2}   ## 索引为2一直到最后的元素
    200 888 999 yyy zzz
    root@ubuntu2204:/home/test# echo ${a[@]:2:3}   ## 索引为2后增加3个元素
    200 888 999

     009、替换数组中的元素

    [root@PC1 test2]# array=(a b c a d a k)
    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k
    [root@PC1 test2]# echo ${array[@]/a/QQ}   ## 将数组中的a替换为QQ
    QQ b c QQ d QQ k

    010、向数组中添加元素

    001、

    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k
    [root@PC1 test2]# array=(${array[@]} II RR)
    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR

    002、

    [root@PC1 test2]# echo ${#array[@]}
    9
    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR
    [root@PC1 test2]# array[9]=100
    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR 100

    011、从数组中删除一个元素

    001、

    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR 100
    [root@PC1 test2]# unset array[2]         ## 删除索引为2的数组元素
    [root@PC1 test2]# echo ${array[@]}
    a b a d a k II RR 100

    002、

    [root@PC1 test2]# array=(a b c a d a k)
    [root@PC1 test2]# array=(${array[@]} II RR)
    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR
    [root@PC1 test2]# array=(${array[@]:0:2} ${array[@]:(2+1)})
    [root@PC1 test2]# echo ${array[@]}
    a b a d a k II RR

    003、利用正则删除数组中的元素

    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR
    [root@PC1 test2]# echo ${array[@]/a*/}
    b c d k II RR

    012、复制数组

    [root@PC1 test2]# echo ${array[@]}
    a b c a d a k II RR
    [root@PC1 test2]# array2=(${array[@]})
    [root@PC1 test2]# echo ${array2[@]}
    a b c a d a k II RR

    013、数组的合并

    [root@PC1 test2]# unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux')
    [root@PC1 test2]# shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh')
    [root@PC1 test2]# abc=("${unix[@]}" "${shell[@]}")
    [root@PC1 test2]# echo ${abc[@]}
    Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh

     014、删除数组

    [root@PC1 test2]# array=(a d r c k i)
    [root@PC1 test2]# echo ${array[@]}
    a d r c k i
    [root@PC1 test2]# unset array
    [root@PC1 test2]# echo ${array[@]}

    015、将文件内容加载到数组中

    [root@PC1 test2]# ls
    file.txt
    [root@PC1 test2]# cat file.txt
    aaa
    bbb
    ccc ddd
    [root@PC1 test2]# array=($(cat file.txt))
    [root@PC1 test2]# echo ${array[@]}
    aaa bbb ccc ddd

    参考:https://www.linuxprobe.com/shell-array-example.html

  • 相关阅读:
    C语言II作业01
    C语言寒假大作战04
    C语言寒假大作战03
    C语言寒假大作战02
    C语言寒假大作战01
    C语言I博客作业12—学期总结
    第一次作业
    C语言I博客作业02
    C语言I博客作业11
    C语言||作业01
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/16414298.html
Copyright © 2020-2023  润新知