bash 数组的使用

int32位 posted @ Mar 05, 2014 09:25:11 PM in c/cpp , 2612 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

bash数组不是什么新的东西,网上的资料也很多,我再写,似乎有点多余。但网上都讲的不太清楚,尤其是* @ 和引号之间的区别。

下面先从数组的基本操作开始:

初始化数组, a=(1 2 3 4),这个用多了c语言的尤其容易出错,往往=左右加上空格,( ) 写成{ },中间元素加上多余的逗号等,这都会造成错误,而错误不是语法错误,而是结果不是你所期待的,这才是最危险的!比如写出a=(1,2,3,4),语法没有错误,但结果是一个只有一个元素的数组,元素为"1,2,3,4",a={1 2 3 4 5}则会出现语法错误,解释器会把{1当作一条命令, 数组初始化也可以指定索引,并且可以逻辑不连续存储,即可以这样a=([2]=2 [5]=5),此时a[2]=2, a[5]=5,但a[0],a[1],...等均为空,并且a数组的长度为2而不是6,原因下面讲!

访问某一个元素的值是${array[n]}(前面有美元符号,并且array[n]有花括号括起来,这里不知道为什么不显示)其中array为数组名,n为索引。比如echo ${a[2]}表示输出a数组第3个元素的值。

得到一个数组后就要说数组的长度了,由于bash数组不是静态分配的,具体占多少内存不知道,你完全可以不进行任何声明和初始化,直接输出a[100],没有任何语法错误,只不过它的值为空而已,可以理解成值为“”(注意不是真的是“”,只是好理解而已),而长度仅仅是计算不为空元素的个数,比如a=([2]=2 [5]=5),展开为 "" "" 2 "" "" 5 "" "" .. ,其中""表示空,则这个数组长度为2,而不是5也不是6. 输出数组长度的关键符号是#,比如获取a数组的长度,语法是${#a}吗,错误。。。这个又需要了解两个符号* 和@。

先讲讲#的原理,#可以看作是一个函数,这个函数输出它后面参数并以IFS定义为分隔符的个数,比如 # 1 2 3 4  5, 返回5(IFS默认为空白符),而#  "1 2" "3 4 5" 6返回3,引号内为一个整体,请注意,这样只是为了好理解,并不是说bash真的有这样的语法!

*的作用是把一个数组的所有元素当做一个整体返回,比如 * 1 2 3 4 5 ,返回 "1 2 3 4 5",注意""只表示这是一个整体,并不是真的会有引号,但确实是当做一个字符串处理的。

@的作用是把数组的所有元素逐一返回, 比如 @ 1 2 3 4 5 ,返回 "1" "2" "3" "4" "5",同样注意""只是记号!

好了,假设a={1 2 3 4 5}

,则${#a}输出什么呢?答案是1,为什么呢?注意a后面没有索引符号[ ],则默认为输出a的值,而注意,bash中a和a[0]的值完全等价,即

b=([0]=2),此时$b为2,而c=5,echo ${c[0])也为5,这个学过c应该容易理解.

因此${#a}相当于${#a[0]},展开为# 1 返回1,相当于求a[0]元素的长度。而要求整个数组长度,应该使用* 和 @,

即${#a[*]}, 展开为 # 1 2 3 4 5 ,结果为5,当然使用@返回结果也是一样的。

下面讲讲系统内置数组,即位置参数,shell中相当于传递的命令行参数,而函数,则相当于传递的参数。这个数组是匿名的,即没有名字,访问第3个元素,则${3},[ ]也要省略, 或者直接$3,求长度为${#},简写$#,其他$@ 和 $* 同理。当然对于这个位置参数,系统提供了更丰富的操作,比如shift等。

如果一个元素使用引号引起来,则相当于一个元素,即a={"1 2 3 4 5"},它的长度为1.

接下来看切片,学过python就不会陌生了,这个和python类似,${a[@]:1}表示返回索引1(包括1)开始以后的所有元素,${a[@]:1:2}表示从索引1开始,包括1后面的2个元素,a={1 2 3 4 5}, ${a[@]:1:2}返回2 3。

元素替换,这个和vim的操作类似,比如要把a的所有元素中的2替换成3,则${a[@]/2/3}.

删除元素,使用unset,比如删除第2个元素则unset a[1],注意删除元素但并不会移动元素,只是简单的把该索引的元素置空,原来的元素位置不变。

好了,数组的基本语法基本讲完了,下面看看以下代码:

#!/bin/bash
a=(1 2 3 4 5)
b=("1 2" "3 4" 5)
echo "le a is ${#a[*]}" # 5
echo "len a is ${#a[@]}" # 5
echo "len b is ${#b[*]}" # 3
echo "len b is ${#b[@]}" # 3
c=(${b[*]})
d=(${b[*]})
echo "len c is ${#c[*]}" #5
echo "len d is ${#d[*]}" #5
e=("${b[*]}")
f=("${b[*]}")
echo "len e is ${#e[*]}" #1
echo "len f is ${#f[*]}" #1

g=(${b[@]})
h=(${b[@]})
echo "len g is ${#g[*]}" #5
echo "len h is ${#h[*]}" #5
i=("${b[@]}")
j=("${b[@]}")
echo "len i is ${#i[*]}" #3
echo "len j is ${#j[*]}" #3

 

 

转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
Digital Ali 说:
2021年9月04日 18:41

very interesting post.this is my first time visit here.i found so mmany interesting stuff in your blog especially its discussion..thanks for the post! soap 2 day

Gullam Mohiyoddin 说:
2021年9月28日 21:36

Sees for paper an especially solid perspective, I staggered close by your blog other than clear out up a kept transmission. I need your viewpoint of drawing... เว็บ 888

Gullam Mohiyoddin 说:
2021年9月29日 02:23

For this site, you will see our record, endeavor to encounter this data. เสือมังกร วิธีเล่น

William 说:
2021年9月30日 02:05

For this site, you will see our record, endeavor to encounter this data. starvegas

William 说:
2021年9月30日 17:40

I use as demonstrated by a general point of view unparalleled surfaces : you will discover these things by: บาคาร่า ฟรี

William 说:
2021年9月30日 23:47

This is interfacing with, at long last it is major for you to visit this specific url: SA

William Johnson 说:
2021年10月02日 18:26

I can propose as shown by an overall perspective beast and wonderfully gifted tips, as shown by such's point of view: metamorphosis literary agency

William Johnson 说:
2021年10月02日 23:47

I went onto your blog while focusing just unassumingly submits. Dazzling improvement for in a moderate second, I will be bookmarking quickly handle your full scale trips... แทงหวย

Dave 说:
2021年10月06日 19:12

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up. PG SLOT

link 说:
2021年10月10日 20:59

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. North American Bancard Agent Program

William Johnson 说:
2021年10月22日 12:06

I should say on an amazingly chief level that its bobbling! The blog is edifying and perseveringly produce puzzling things. 우리카지노

AP 10th Science Ques 说:
2022年9月17日 02:03

AP 10th Class Science Model Paper 2023 Pdf Download may useful to both Telugu Medium, English Medium and Urdu Medium 10th class students of the state board to score better marks in the exams like SA-1, SA-2, FA-1, FA-2, FA-3, FA-4. AP 10th Science Question PaperThese practice model papers not only helped in getting marks but also improve the student’s knowledge of science.AP 10th Science Model Paper 2023 Pdf with answers suggested for all kinds of exams held under BSEAP along with assignments were made with the instructions of Leading educational institutes, Education portals of the state such as Sakshi Education, Eenadu Pratibha.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter