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

📄 arrays.html

📁 BASH Shell 编程 经典教程 《高级SHELL脚本编程》中文版
💻 HTML
📖 第 1 页 / 共 5 页
字号:
 52&nbsp;# 子串替换 53&nbsp; 54&nbsp;# 第一个匹配到的子串将会被替换 55&nbsp;echo ${arrayZ[@]/fiv/XYZ}   # one two three four XYZe XYZe 56&nbsp;                            # 匹配将应用于数组的所有元素.  57&nbsp; 58&nbsp;# 所有匹配到的子串都会被替换 59&nbsp;echo ${arrayZ[@]//iv/YY}    # one two three four fYYe fYYe 60&nbsp;                            # 匹配将应用于数组的所有元素.  61&nbsp; 62&nbsp;# 删除所有的匹配子串 63&nbsp;# 如果没有指定替换字符串的话, 那就意味着'删除' 64&nbsp;echo ${arrayZ[@]//fi/}      # one two three four ve ve 65&nbsp;                            # 匹配将应用于数组的所有元素.  66&nbsp; 67&nbsp;# 替换字符串前端子串 68&nbsp;echo ${arrayZ[@]/#fi/XY}    # one two three four XYve XYve 69&nbsp;                            # 匹配将应用于数组的所有元素.  70&nbsp; 71&nbsp;# 替换字符串后端子串 72&nbsp;echo ${arrayZ[@]/%ve/ZZ}    # one two three four fiZZ fiZZ 73&nbsp;                            # 匹配将应用于数组的所有元素.  74&nbsp; 75&nbsp;echo ${arrayZ[@]/%o/XX}     # one twXX three four five five 76&nbsp;                            # 为什么?  77&nbsp; 78&nbsp;echo "-----------------------" 79&nbsp; 80&nbsp; 81&nbsp;# 在将处理后的结果发送到awk(或者其他的处理工具)之前 -- 82&nbsp;# 回忆一下: 83&nbsp;#   $( ... )是命令替换.  84&nbsp;#   函数作为子进程运行.  85&nbsp;#   函数结果输出到stdout.  86&nbsp;#   用read来读取函数的stdout.  87&nbsp;#   使用name[@]表示法指定了一个"for-each"操作.  88&nbsp; 89&nbsp;newstr() { 90&nbsp;    echo -n "!!!" 91&nbsp;} 92&nbsp; 93&nbsp;echo ${arrayZ[@]/%e/$(newstr)} 94&nbsp;# on!!! two thre!!! four fiv!!! fiv!!! 95&nbsp;# Q.E.D: 替换动作实际上是一个'赋值'.  96&nbsp; 97&nbsp;#  使用"For-Each"形式的 98&nbsp;echo ${arrayZ[@]//*/$(newstr optional_arguments)} 99&nbsp;#  现在, 如果Bash只将匹配到的子串作为$0100&nbsp;#+ 传递给将被调用的函数 . . .101&nbsp;102&nbsp;echo103&nbsp;104&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P><AHREF="commandsub.html#COMMANDSUBREF">命令替换</A>可以构造数组的独立元素. 		  (译者注: 换句话说, 就是命令替换也能够给数组赋值.) </P><DIVCLASS="EXAMPLE"><HR><ANAME="SCRIPTARRAY"></A><P><B>例子 26-5. 将脚本的内容赋值给数组</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# script-array.sh: 将这个脚本的内容赋值给数组.   3&nbsp;# 这个脚本的灵感来自于Chris Martin的e-mail(感谢!).   4&nbsp;  5&nbsp;script_contents=( $(cat "$0") )  #  将这个脚本的内容($0)  6&nbsp;                                 #+ 赋值给数组.   7&nbsp;  8&nbsp;for element in $(seq 0 $((${#script_contents[@]} - 1)))  9&nbsp;  do                #  ${#script_contents[@]} 10&nbsp;                    #+ 表示数组元素的个数.  11&nbsp;                    # 12&nbsp;                    #  一个小问题: 13&nbsp;                    #  为什么必须使用seq 0?  14&nbsp;                    #  用seq 1来试一下.  15&nbsp;  echo -n "${script_contents[$element]}" 16&nbsp;                    # 在同一行上显示脚本中每个域的内容.  17&nbsp;  echo -n " -- "    # 使用 " -- " 作为域分割符.  18&nbsp;done 19&nbsp; 20&nbsp;echo 21&nbsp; 22&nbsp;exit 0 23&nbsp; 24&nbsp;# 练习: 25&nbsp;# ----- 26&nbsp;#  修改这个脚本,  27&nbsp;#+ 让这个脚本能够按照它原本的格式输出,  28&nbsp;#+ 连同空白, 换行, 等等. </PRE></FONT></TD></TR></TABLE><HR></DIV><P>在数组环境中, 某些Bash<AHREF="internal.html#BUILTINREF">内建命令</A>的含义可能会有些轻微的改变. 	比如, <AHREF="internal.html#UNSETREF">unset</A>命令可以删除数组元素, 	甚至能够删除整个数组. </P><DIVCLASS="EXAMPLE"><HR><ANAME="EX67"></A><P><B>例子 26-6. 一些数组专用的小道具</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;  3&nbsp;declare -a colors  4&nbsp;#  脚本中所有的后续命令都会把  5&nbsp;#+ 变量"colors"看作数组.   6&nbsp;  7&nbsp;echo "Enter your favorite colors (separated from each other by a space)."  8&nbsp;  9&nbsp;read -a colors    # 至少需要键入3种颜色, 以便于后边的演示.  10&nbsp;#  'read'命令的特殊选项,  11&nbsp;#+ 允许给数组元素赋值.  12&nbsp; 13&nbsp;echo 14&nbsp; 15&nbsp;element_count=${#colors[@]} 16&nbsp;# 提取数组元素个数的特殊语法.  17&nbsp;#     用element_count=${#colors[*]}也一样.  18&nbsp;# 19&nbsp;#  "@"变量允许在引用中存在单词分割(word splitting) 20&nbsp;#+ (依靠空白字符来分隔变量).  21&nbsp;# 22&nbsp;#  这就好像"$@"和"$*" 23&nbsp;#+ 在位置参数中的所表现出来的行为一样.  24&nbsp; 25&nbsp;index=0 26&nbsp; 27&nbsp;while [ "$index" -lt "$element_count" ] 28&nbsp;do    # 列出数组中的所有元素.  29&nbsp;  echo ${colors[$index]} 30&nbsp;  let "index = $index + 1" 31&nbsp;  # 或: 32&nbsp;  #    index+=1 33&nbsp;  # 如果你运行的Bash版本是3.1以后的话, 才支持这种语法.  34&nbsp;done 35&nbsp;# 每个数组元素被列为单独的一行.  36&nbsp;# 如果没有这种要求的话, 可以使用echo -n "${colors[$index]} " 37&nbsp;# 38&nbsp;# 也可以使用"for"循环来做:  39&nbsp;#   for i in "${colors[@]}" 40&nbsp;#   do 41&nbsp;#     echo "$i" 42&nbsp;#   done 43&nbsp;# (感谢, S.C.) 44&nbsp; 45&nbsp;echo 46&nbsp; 47&nbsp;# 再次列出数组中的所有元素, 不过这次的做法更优雅.  48&nbsp;  echo ${colors[@]}          # 用echo ${colors[*]}也行.  49&nbsp; 50&nbsp;echo 51&nbsp; 52&nbsp;# "unset"命令即可以删除数组数据, 也可以删除整个数组.  53&nbsp;unset colors[1]              # 删除数组的第2个元素.  54&nbsp;                             # 作用等效于   colors[1]= 55&nbsp;echo  ${colors[@]}           # 再次列出数组内容, 第2个元素没了.  56&nbsp; 57&nbsp;unset colors                 # 删除整个数组.  58&nbsp;                             #  unset colors[*] 或 59&nbsp;                             #+ unset colors[@] 都可以.  60&nbsp;echo; echo -n "Colors gone."			    61&nbsp;echo ${colors[@]}            # 再次列出数组内容, 内容为空.  62&nbsp; 63&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P>正如我们在前面例子中所看到的, 	<BCLASS="COMMAND">${array_name[@]}</B>或<BCLASS="COMMAND">${array_name[*]}</B>都与数组中的<EM>所有</EM>元素相关. 	同样的, 为了计算数组的元素个数, 	可以使用<BCLASS="COMMAND">${#array_name[@]}</B>或<BCLASS="COMMAND">${#array_name[*]}</B>. 	<BCLASS="COMMAND">${#array_name}</B>是数组第一个元素的长度, 	也就是<BCLASS="COMMAND">${array_name[0]}</B>的长度(字符个数). 	</P><DIVCLASS="EXAMPLE"><HR><ANAME="EMPTYARRAY"></A><P><B>例子 26-7. 空数组与包含空元素的数组</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;#!/bin/bash  2&nbsp;# empty-array.sh  3&nbsp;  4&nbsp;#  感谢Stephane Chazelas制作这个例子的原始版本,   5&nbsp;#+ 同时感谢Michael Zick对这个例子所作的扩展.   6&nbsp;  7&nbsp;  8&nbsp;# 空数组与包含有空元素的数组, 这两个概念不同.   9&nbsp; 10&nbsp;array0=( first second third ) 11&nbsp;array1=( '' )   # "array1"包含一个空元素.  12&nbsp;array2=( )      # 没有元素 . . . "array2"为空.  13&nbsp; 14&nbsp;echo 15&nbsp;ListArray() 16&nbsp;{ 17&nbsp;echo 18&nbsp;echo "Elements in array0:  ${array0[@]}" 19&nbsp;echo "Elements in array1:  ${array1[@]}" 20&nbsp;echo "Elements in array2:  ${array2[@]}" 21&nbsp;echo 22&nbsp;echo "Length of first element in array0 = ${#array0}" 23&nbsp;echo "Length of first element in array1 = ${#array1}" 24&nbsp;echo "Length of first element in array2 = ${#array2}" 25&nbsp;echo 26&nbsp;echo "Number of elements in array0 = ${#array0[*]}"  # 3 27&nbsp;echo "Number of elements in array1 = ${#array1[*]}"  # 1  (惊奇!) 28&nbsp;echo "Number of elements in array2 = ${#array2[*]}"  # 0 29&nbsp;} 30&nbsp; 31&nbsp;# =================================================================== 32&nbsp; 33&nbsp;ListArray 34&nbsp; 35&nbsp;# 尝试扩展这些数组.  36&nbsp; 37&nbsp;# 添加一个元素到这个数组.  38&nbsp;array0=( "${array0[@]}" "new1" ) 39&nbsp;array1=( "${array1[@]}" "new1" ) 40&nbsp;array2=( "${array2[@]}" "new1" ) 41&nbsp; 42&nbsp;ListArray 43&nbsp; 44&nbsp;# 或 45&nbsp;array0[${#array0[*]}]="new2" 46&nbsp;array1[${#array1[*]}]="new2" 47&nbsp;array2[${#array2[*]}]="new2" 48&nbsp; 49&nbsp;ListArray 50&nbsp; 51&nbsp;# 如果你按照上边的方法对数组进行扩展的话; 数组比较象'栈'  52&nbsp;# 上边的操作就是'压栈'  53&nbsp;# 栈'高'为:  54&nbsp;height=${#array2[@]} 55&nbsp;echo 56&nbsp;echo "Stack height for array2 = $height" 57&nbsp; 58&nbsp;# '出栈'就是:  59&nbsp;unset array2[${#array2[@]}-1]	#  数组从0开始索引,  60&nbsp;height=${#array2[@]}            #+ 这意味着第一个数组下标为0.  61&nbsp;echo 62&nbsp;echo "POP" 63&nbsp;echo "New stack height for array2 = $height" 64&nbsp; 65&nbsp;ListArray 66&nbsp; 67&nbsp;# 只列出数组array0的第二个和第三个元素. 68&nbsp;from=1		# 从0开始索引.  69&nbsp;to=2		# 70&nbsp;array3=( ${array0[@]:1:2} ) 71&nbsp;echo 72&nbsp;echo "Elements in array3:  ${array3[@]}" 73&nbsp; 74&nbsp;# 处理方式就像是字符串(字符数组).  75&nbsp;# 试试其他的"字符串"形式.  76&nbsp; 77&nbsp;# 替换:  78&nbsp;array4=( ${array0[@]/second/2nd} ) 79&nbsp;echo 80&nbsp;echo "Elements in array4:  ${array4[@]}" 81&nbsp; 82&nbsp;# 替换掉所有匹配通配符的字符串.  83&nbsp;array5=( ${array0[@]//new?/old} ) 84&nbsp;echo 85&nbsp;echo "Elements in array5:  ${array5[@]}" 86&nbsp; 87&nbsp;# 当你开始觉得对此有把握的时候 . . . 88&nbsp;array6=( ${array0[@]#*new} ) 89&nbsp;echo # 这个可能会让你感到惊奇.  90&nbsp;echo "Elements in array6:  ${array6[@]}" 91&nbsp; 92&nbsp;array7=( ${array0[@]#new1} ) 93&nbsp;echo # 数组array6之后就没有惊奇了.  94&nbsp;echo "Elements in array7:  ${array7[@]}" 95&nbsp; 96&nbsp;# 看起来非常像 . . . 97&nbsp;array8=( ${array0[@]/new1/} ) 98&nbsp;echo 99&nbsp;echo "Elements in array8:  ${array8[@]}"100&nbsp;101&nbsp;#  所以, 让我们怎么形容呢? 102&nbsp;103&nbsp;#  对数组var[@]中的每个元素104&nbsp;#+ 进行连续的字符串操作. 105&nbsp;#  因此: 如果结果是长度为0的字符串, 106&nbsp;#+ Bash支持字符串向量操作, 107&nbsp;#+ 元素会在结果赋值中消失不见. 108&nbsp;109&nbsp;#  一个问题, 这些字符串是强引用还是弱引用? 110&nbsp;111&nbsp;zap='new*'112&nbsp;array9=( ${array0[@]/$zap/} )113&nbsp;echo114&nbsp;echo "Elements in array9:  ${array9[@]}"115&nbsp;116&nbsp;# 当你还在考虑, 你身在Kansas州何处时 . . .117&nbsp;array10=( ${array0[@]#$zap} )118&nbsp;echo119&nbsp;echo "Elements in array10:  ${array10[@]}"120&nbsp;121&nbsp;# 比较array7和array10. 122&nbsp;# 比较array8和array9. 123&nbsp;124&nbsp;# 答案: 必须是弱引用. 125&nbsp;126&nbsp;exit 0</PRE></FONT></TD></TR></TABLE><HR></DIV><P><BCLASS="COMMAND">${array_name[@]}</B>和<BCLASS="COMMAND">${array_name[*]}</B>的关系非常类似于<AHREF="internalvariables.html#APPREF">$@ and $*</A>. 		 这种数组用法用处非常广泛. </P><P>      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><FONTCOLOR="#000000"><PRECLASS="PROGRAMLISTING">  1&nbsp;# 复制一个数组.   2&nbsp;array2=( "${array1[@]}" )  3&nbsp;# 或  4&nbsp;array2="${array1[@]}"  5&nbsp;#  6&nbsp;#  然而, 如果在"缺项"数组中使用的话, 将会失败,   7&nbsp;#+ 也就是说数组中存在空洞(中间的某个元素没赋值),   8&nbsp;#+ 这个问题由Jochen DeSmet指出.   9&nbsp;# ------------------------------------------ 10&nbsp;  array1[0]=0 11&nbsp;# array1[1]没赋值 12&nbsp;  array1[2]=2 13&nbsp;  array2=( "${array1[@]}" )       # 拷贝它?  14&nbsp; 15&nbsp;echo ${array2[0]}      # 0 16&nbsp;echo ${array2[2]}      # (null), 应该是2 17&nbsp;# ------------------------------------------ 18&nbsp; 19&nbsp; 20&nbsp; 21&nbsp;# 添加一个元素到数组.  22&nbsp;array=( "${array[@]}" "new element" ) 23&nbsp;# 或 24&nbsp;array[${#array[*]}]="new element" 25&nbsp; 26&nbsp;# 感谢, S.C.</PRE></FONT></TD></TR></TABLE>      </P><DIVCLASS="TIP"><P></P><TABLECLASS="TIP"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="./images/tip.gif"HSPACE="5"ALT="Tip"></TD><TD

⌨️ 快捷键说明

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