Linux shell数组与关联数组的用法实例

2019-02-1908:22:29后端程序开发Comments2,902 views字数 656阅读模式

1. 关联数组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

  • 使用 declare -A(declare 的用法请使用 help 进行查看,help declare) 进行声明关联数组变量;
$ declare -A fruits_price
$ fruits_price=([apple]='$100' [orange]='$150')
  • 列出关联数组的索引(也就是 key):
$ echo ${!fruits_price[*]}
$ echo ${!fruits_price[@]}

2. 序列数组文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

seq 方法创建文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

  • 基本用法:
$ a_num_seq = ($seq 5)
$ echo $a_num_seq
1 2 3 4 5
  • a_num_seq 得到是字符串,不同之处以空格分隔开。在linux 里面,可以把它看作是 list. 可以通过for…in 循环读取。
$ for i in $a_num_list; do echo $i; done;
1
2
3
4
5
  • 生成 array,只需在$(seq 5)外再套一层()
$ a_num_seq = ($(seq 5))
$ echo $a_num_seq
1
$ echo ${#a_num_seq[@]}
5 # 得到其长度信息
  • 使用 {begin…end}

注意 begin 和 and 之间是两个小数点,而不是三个;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

$ echo {1..10}
1 2 3 4 5 6 7 8 9 10
$ for i in {1..5}; do echo $i; done;
1
2
3
4
5
  • 性能比较
$ time echo {1..100000}

real 0m18.758s
user 0m0.068s
sys 0m0.012s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

$ time echo $(seq 100000)

real 0m20.064s
user 0m0.068s
sys 0m0.012s文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/9627.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/9627.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定