⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrays.html

📁 BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>数组</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINKREL="HOME"TITLE="高级Bash脚本编程指南"HREF="index.html"><LINKREL="UP"TITLE="高级主题"HREF="part4.html"><LINKREL="PREVIOUS"TITLE="列表结构"HREF="list-cons.html"><LINKREL="NEXT"TITLE="/dev和/proc"HREF="devproc.html"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">高级Bash脚本编程指南: 一本深入学习shell脚本艺术的书籍</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="list-cons.html"ACCESSKEY="P">前一页</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="devproc.html"ACCESSKEY="N">下一页</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="ARRAYS"></A>26. 数组</H1><P><ANAME="ARRAYREF"></A></P><P>新版本的Bash支持一维数组. 	数组元素可以使用符号<KBDCLASS="USERINPUT">variable[xx]</KBD>来初始化. 	另外, 脚本可以使用<KBDCLASS="USERINPUT">declare -a variable</KBD>语句来指定一个数组. 	如果想解引用一个数组元素(也就是取值), 	可以使用<ICLASS="FIRSTTERM">大括号</I>, 	访问形式为<KBDCLASS="USERINPUT">${variable[xx]}</KBD>. </P><DIVCLASS="EXAMPLE"><HR><ANAME="EX66"></A><P><B>例子 26-1. 简单的数组使用</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;  3&nbsp;  4&nbsp;area[11]=23  5&nbsp;area[13]=37  6&nbsp;area[51]=UFOs  7&nbsp;  8&nbsp;#  数组成员不一定非得是相邻或连续的.   9&nbsp; 10&nbsp;#  数组的部分成员可以不被初始化.  11&nbsp;#  数组中允许空缺元素.  12&nbsp;#  实际上, 保存着稀疏数据的数组("稀疏数组") 13&nbsp;#+ 在电子表格处理软件中是非常有用的.  14&nbsp; 15&nbsp; 16&nbsp;echo -n "area[11] = " 17&nbsp;echo ${area[11]}    #  需要{大括号}.  18&nbsp; 19&nbsp;echo -n "area[13] = " 20&nbsp;echo ${area[13]} 21&nbsp; 22&nbsp;echo "Contents of area[51] are ${area[51]}." 23&nbsp; 24&nbsp;# 没被初始化的数组成员打印为空值(null变量).  25&nbsp;echo -n "area[43] = " 26&nbsp;echo ${area[43]} 27&nbsp;echo "(area[43] unassigned)" 28&nbsp; 29&nbsp;echo 30&nbsp; 31&nbsp;# 两个数组元素的和被赋值给另一个数组元素 32&nbsp;area[5]=`expr ${area[11]} + ${area[13]}` 33&nbsp;echo "area[5] = area[11] + area[13]" 34&nbsp;echo -n "area[5] = " 35&nbsp;echo ${area[5]} 36&nbsp; 37&nbsp;area[6]=`expr ${area[11]} + ${area[51]}` 38&nbsp;echo "area[6] = area[11] + area[51]" 39&nbsp;echo -n "area[6] = " 40&nbsp;echo ${area[6]} 41&nbsp;# 这里会失败, 是因为不允许整数与字符串相加.  42&nbsp; 43&nbsp;echo; echo; echo 44&nbsp; 45&nbsp;# ----------------------------------------------------------------- 46&nbsp;# 另一个数组, "area2". 47&nbsp;# 另一种给数组变量赋值的方法... 48&nbsp;# array_name=( XXX YYY ZZZ ... ) 49&nbsp; 50&nbsp;area2=( zero one two three four ) 51&nbsp; 52&nbsp;echo -n "area2[0] = " 53&nbsp;echo ${area2[0]} 54&nbsp;# 阿哈, 从0开始计算数组下标(也就是数组的第一个元素为[0], 而不是[1]).  55&nbsp; 56&nbsp;echo -n "area2[1] = " 57&nbsp;echo ${area2[1]}    # [1]是数组的第2个元素.  58&nbsp;# ----------------------------------------------------------------- 59&nbsp; 60&nbsp;echo; echo; echo 61&nbsp; 62&nbsp;# ----------------------------------------------- 63&nbsp;# 第3个数组, "area3". 64&nbsp;# 第3种给数组元素赋值的方法... 65&nbsp;# array_name=([xx]=XXX [yy]=YYY ...) 66&nbsp; 67&nbsp;area3=([17]=seventeen [24]=twenty-four) 68&nbsp; 69&nbsp;echo -n "area3[17] = " 70&nbsp;echo ${area3[17]} 71&nbsp; 72&nbsp;echo -n "area3[24] = " 73&nbsp;echo ${area3[24]} 74&nbsp;# ----------------------------------------------- 75&nbsp; 76&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><DIVCLASS="NOTE"><P></P><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="./images/note.gif"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>Bash允许把变量当成数组来操作, 		即使这个变量没有明确地被声明为数组. 	  <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;string=abcABC123ABCabc  2&nbsp;echo ${string[@]}               # abcABC123ABCabc  3&nbsp;echo ${string[*]}               # abcABC123ABCabc   4&nbsp;echo ${string[0]}               # abcABC123ABCabc  5&nbsp;echo ${string[1]}               # 没有输出!   6&nbsp;                                # 为什么?   7&nbsp;echo ${#string[@]}              # 1  8&nbsp;                                # 数组中只有一个元素.   9&nbsp;                                # 就是这个字符串本身.  10&nbsp; 11&nbsp;# 感谢, Michael Zick, 指出这一点. </PRE></FONT></TD></TR></TABLE>      类似的示范可以参考<AHREF="untyped.html#BVUNTYPED">Bash变量是无类型的</A>.       </P></TD></TR></TABLE></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="POEM"></A><P><B>例子 26-2. 格式化一首诗</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# poem.sh: 将本书作者非常喜欢的一首诗, 漂亮的打印出来.   3&nbsp;  4&nbsp;# 诗的行数(单节).   5&nbsp;Line[1]="I do not know which to prefer,"  6&nbsp;Line[2]="The beauty of inflections"  7&nbsp;Line[3]="Or the beauty of innuendoes,"  8&nbsp;Line[4]="The blackbird whistling"  9&nbsp;Line[5]="Or just after." 10&nbsp; 11&nbsp;# 出处.  12&nbsp;Attrib[1]=" Wallace Stevens" 13&nbsp;Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\"" 14&nbsp;# 这首诗已经是公共版权了(版权已经过期了).  15&nbsp; 16&nbsp;echo 17&nbsp; 18&nbsp;for index in 1 2 3 4 5    # 5行.  19&nbsp;do 20&nbsp;  printf "     %s\n" "${Line[index]}" 21&nbsp;done 22&nbsp; 23&nbsp;for index in 1 2          # 出处为2行.  24&nbsp;do 25&nbsp;  printf "          %s\n" "${Attrib[index]}" 26&nbsp;done 27&nbsp; 28&nbsp;echo 29&nbsp; 30&nbsp;exit 0 31&nbsp; 32&nbsp;# 练习: 33&nbsp;# ----- 34&nbsp;# 修改这个脚本, 使其能够从一个文本数据文件中提取出一首诗的内容, 然后将其漂亮的打印出来. </PRE></FONT></TD></TR></TABLE><HR></DIV><P>数组元素有它们独特的语法, 	甚至标准Bash命令和操作符, 都有特殊的选项用以配合数组操作. 	</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARRAYOPS"></A><P><B>例子 26-3. 多种数组操作</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# array-ops.sh: 更多有趣的数组用法.   3&nbsp;  4&nbsp;  5&nbsp;array=( zero one two three four five )  6&nbsp;# 数组元素 0   1   2    3     4    5  7&nbsp;  8&nbsp;echo ${array[0]}       #  0  9&nbsp;echo ${array:0}        #  0 10&nbsp;                       #  第一个元素的参数扩展,  11&nbsp;                       #+ 从位置0(#0)开始(即第一个字符).  12&nbsp;echo ${array:1}        #  ero 13&nbsp;                       #  第一个元素的参数扩展,  14&nbsp;                       #+ 从位置1(#1)开始(即第2个字符).  15&nbsp; 16&nbsp;echo "--------------" 17&nbsp; 18&nbsp;echo ${#array[0]}      #  4 19&nbsp;                       #  第一个数组元素的长度.  20&nbsp;echo ${#array}         #  4 21&nbsp;                       #  第一个数组元素的长度.  22&nbsp;                       #  (另一种表示形式) 23&nbsp; 24&nbsp;echo ${#array[1]}      #  3 25&nbsp;                       #  第二个数组元素的长度.  26&nbsp;                       #  Bash中的数组是从0开始索引的.  27&nbsp; 28&nbsp;echo ${#array[*]}      #  6 29&nbsp;                       #  数组中的元素个数.  30&nbsp;echo ${#array[@]}      #  6 31&nbsp;                       #  数组中的元素个数. 32&nbsp; 33&nbsp;echo "--------------" 34&nbsp; 35&nbsp;array2=( [0]="first element" [1]="second element" [3]="fourth element" ) 36&nbsp; 37&nbsp;echo ${array2[0]}      # 第一个元素 38&nbsp;echo ${array2[1]}      # 第二个元素 39&nbsp;echo ${array2[2]}      # 40&nbsp;                       # 因为并没有被初始化, 所以此值为null.  41&nbsp;echo ${array2[3]}      # 第四个元素 42&nbsp; 43&nbsp; 44&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P>大部分标准<AHREF="string-manipulation.html#STRINGMANIP">字符串操作</A>都可以用于数组中. </P><DIVCLASS="EXAMPLE"><HR><ANAME="ARRAYSTROPS"></A><P><B>例子 26-4. 用于数组的字符串操作</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# array-strops.sh: 用于数组的字符串操作.   3&nbsp;# 本脚本由Michael Zick所编写.   4&nbsp;# 授权使用在本书中.   5&nbsp;  6&nbsp;#  一般来说, 任何类似于${name ... }(这种形式)的字符串操作  7&nbsp;#+ 都能够应用于数组中的所有字符串元素,   8&nbsp;#+ 比如说${name[@] ... }或${name[*] ...}这两种形式.   9&nbsp; 10&nbsp; 11&nbsp;arrayZ=( one two three four five five ) 12&nbsp; 13&nbsp;echo 14&nbsp; 15&nbsp;# 提取尾部的子串 16&nbsp;echo ${arrayZ[@]:0}     # one two three four five five 17&nbsp;                        # 所有元素.  18&nbsp; 19&nbsp;echo ${arrayZ[@]:1}     # two three four five five 20&nbsp;                        # element[0]后边的所有元素.  21&nbsp; 22&nbsp;echo ${arrayZ[@]:1:2}   # two three 23&nbsp;                        # 只提取element[0]后边的两个元素.  24&nbsp; 25&nbsp;echo "-----------------------" 26&nbsp; 27&nbsp;#  子串删除 28&nbsp;#  从字符串的开头删除最短的匹配,  29&nbsp;#+ 匹配的子串也可以是正则表达式.  30&nbsp; 31&nbsp;echo ${arrayZ[@]#f*r}   # one two three five five 32&nbsp;                        # 匹配将应用于数组的所有元素.  33&nbsp;                        # 匹配到了"four", 并且将它删除.  34&nbsp; 35&nbsp;# 从字符串的开头删除最长的匹配 36&nbsp;echo ${arrayZ[@]##t*e}  # one two four five five 37&nbsp;                        # 匹配将应用于数组的所有元素.  38&nbsp;                        # 匹配到了"three", 并且将它删除.  39&nbsp; 40&nbsp;# 从字符串的结尾删除最短的匹配 41&nbsp;echo ${arrayZ[@]%h*e}   # one two t four five five 42&nbsp;                        # 匹配将应用于数组的所有元素.  43&nbsp;                        # 匹配到了"hree", 并且将它删除.  44&nbsp; 45&nbsp;# 从字符串的结尾删除最长的匹配 46&nbsp;echo ${arrayZ[@]%%t*e}  # one two four five five 47&nbsp;                        # 匹配将应用于数组的所有元素.  48&nbsp;                        # 匹配到了"three", 并且将它删除.  49&nbsp; 50&nbsp;echo "-----------------------" 51&nbsp;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -