设为首页 收藏本站
查看: 604|回复: 0

shell 常规数组&关联数组

[复制链接]

尚未签到

发表于 2018-8-24 12:29:28 | 显示全部楼层 |阅读模式
  在日常编写shell的过程中,数组是一个非常常用到的内容。我们这里简要介绍两种数组的使用方式。一种常规数组,一个中关联数组。差异就是常规数组,只能用整数作为下标来进行数据的存取。而关联数组能够使用字符作为小标来进行存储。
  常规数组
  1)  初始化
  数组中的多个变量用括号来括起来,变量间用空格来间隔开来。
  root@docker-host-03:~/test_shell#./demo1.sh
  1 2 3 4 5
  1 2 3 4
  root@docker-host-03:~/test_shell# moredemo1.sh
  #!/bin/bash
  a=( 1 2 3 4 5 )
  b=(1
  2
  3
  4
  )
  #Print the arry
  echo ${a
  • }
      echo ${b
  • }
      将文本的内容以行为单位存到数组中
      root@docker-host-03:~/test_shell# vim list
      root@docker-host-03:~/test_shell# a=(`catlist`)
      root@docker-host-03:~/test_shell# echo${a
  • }
      line2 line3 line4 line5 line5 line6
      root@docker-host-03:~/test_shell# echo${a[1]}
      line3
      root@docker-host-03:~/test_shell# cat list
      line2
      line3
      line4
      line5
      line5
      line6
      这里再多提一个就是循环遍历文本中的每一个行
      root@docker-host-03:~/test_shell# whileread line;do echo $line ;done < list
      line2
      line3
      line4
      line5
      line5
      line6
      root@docker-host-03:~/test_shell#./demo1.sh
      do something: line2
      do something: line3
      do something: line4
      do something: line5
      do something: line5
      do something: line6
      do something:
      root@docker-host-03:~/test_shell# moredemo1.sh
      #!/bin/bash
      cat list | while read line;do
      echo "do something: $line"
      done
      2)数据存取
      获取某个指定的变量
      root@docker-host-03:~/test_shell# echo${a[1]}
      line3
      root@docker-host-03:~/test_shell# echo${a[2]}
      line4
      获取变量所有值
      root@docker-host-03:~/test_shell# echo${a[@]}
      line2 line3 line4 line5 line5 line6
      root@docker-host-03:~/test_shell# echo${a
  • }
      line2 line3 line4 line5 line5 line6
      root@docker-host-03:~/test_shell#
      倒序的获取数组变量
      root@docker-host-03:~/test_shell# echo${a[-2]}
      line5
      root@docker-host-03:~/test_shell# echo${a[-4]}
      line4
      获取数组的长度
      root@docker-host-03:~/test_shell# echo${#a
  • }
      6
      root@docker-host-03:~/test_shell# echo${#a[@]}
      6
      2)  赋值&清楚某个小标值
      root@docker-host-03:~/test_shell#echo ${#a[@]}
      6
      root@docker-host-03:~/test_shell#echo ${a
  • }
      line2line3 line4 line5 line5 line6
      root@docker-host-03:~/test_shell#unset a[3]
      root@docker-host-03:~/test_shell#echo ${a
  • }
      line2line3 line4 line5 line6
      root@docker-host-03:~/test_shell#echo ${#a[@]}
      5
      3)  分片存取
      ${数组名[@*]:起始位置:长度}切片原先数组,返回是字符串
      root@docker-host-03:~/test_shell#echo ${a[@]:1:1}
      line3
      root@docker-host-03:~/test_shell#echo ${a[@]:1:2}
      line3 line4
      4)  替换内容
      root@docker-host-03:~/test_shell#echo ${a
  • }
      line2line3 line4 line5 line6
      root@docker-host-03:~/test_shell#echo ${a
  • /2/test}
      linetestline3 line4 line5 line6
      root@docker-host-03:~/test_shell#echo ${a
  • /2/ test}
      line test line3 line4 line5 line6
      root@docker-host-03:~/test_shell#b=(${a
  • /2/ test})
      root@docker-host-03:~/test_shell#echo $b
      line
      root@docker-host-03:~/test_shell#echo ${b[@]}
      line testline3 line4 line5 line6
      root@docker-host-03:~/test_shell#echo ${a[@]}
      line2 line3 line4 line5 line6
      关联数组
      declare参考链接:
      http://blog.csdn.net/tutuboke/article/details/50440598
      关联数组和常规数组的差异就是,关联数组能够使用字符串来作为。其他的操作方法和常规数组是一样的,能分片,能消除。
      初始化
      root@docker-host-03:~#declare -A aa_array
      root@docker-host-03:~#aa_array['index1']='value1'
      root@docker-host-03:~#aa_array['index2']='value2'
      root@docker-host-03:~#aa_array['index3']='value3'
      root@docker-host-03:~#echo ${aa_array[@]}
      value1 value2 value3
      另一种赋值方式
      root@docker-host-03:~#declare  -A a_array=( ['i1']='v1'['i2']='v2' ['i3']='v3' )
      root@docker-host-03:~#echo ${a_array[@]}
      v3 v2 v1


  • 运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
    2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
    3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
    4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
    5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
    6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
    7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
    8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

    所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-555972-1-1.html 上篇帖子: Linux之shell脚本(1) 下篇帖子: shell 脚本扩展
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    扫码加入运维网微信交流群X

    扫码加入运维网微信交流群

    扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

    扫描微信二维码查看详情

    客服E-mail:kefu@iyunv.com 客服QQ:1061981298


    QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


    提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


    本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



    合作伙伴: 青云cloud

    快速回复 返回顶部 返回列表