📄 arrays.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Arrays</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Advanced Topics"HREF="part5.html"><LINKREL="PREVIOUS"TITLE="List Constructs"HREF="list-cons.html"><LINKREL="NEXT"TITLE="/dev and /proc"HREF="devproc.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="list-cons.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="devproc.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="ARRAYS"></A>Chapter 26. Arrays</H1><P><ANAME="ARRAYREF"></A></P><P>Newer versions of Bash support one-dimensional arrays. <ANAME="BRACKARRAY"></A> Array elements may be initialized with the <TTCLASS="USERINPUT"><B>variable[xx]</B></TT> notation. Alternatively, a script may introduce the entire array by an explicit <TTCLASS="USERINPUT"><B>declare -a variable</B></TT> statement. To dereference (find the contents of) an array element, use <ICLASS="FIRSTTERM">curly bracket</I> notation, that is, <TTCLASS="USERINPUT"><B>${variable[xx]}</B></TT>.</P><P><ANAME="ARRAYNOTATION"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX66"></A><P><B>Example 26-1. Simple array usage</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 4 area[11]=23 5 area[13]=37 6 area[51]=UFOs 7 8 # Array members need not be consecutive or contiguous. 9 10 # Some members of the array can be left uninitialized. 11 # Gaps in the array are okay. 12 # In fact, arrays with sparse data ("sparse arrays") 13 #+ are useful in spreadsheet-processing software. 14 15 16 echo -n "area[11] = " 17 echo ${area[11]} # {curly brackets} needed. 18 19 echo -n "area[13] = " 20 echo ${area[13]} 21 22 echo "Contents of area[51] are ${area[51]}." 23 24 # Contents of uninitialized array variable print blank (null variable). 25 echo -n "area[43] = " 26 echo ${area[43]} 27 echo "(area[43] unassigned)" 28 29 echo 30 31 # Sum of two array variables assigned to third 32 area[5]=`expr ${area[11]} + ${area[13]}` 33 echo "area[5] = area[11] + area[13]" 34 echo -n "area[5] = " 35 echo ${area[5]} 36 37 area[6]=`expr ${area[11]} + ${area[51]}` 38 echo "area[6] = area[11] + area[51]" 39 echo -n "area[6] = " 40 echo ${area[6]} 41 # This fails because adding an integer to a string is not permitted. 42 43 echo; echo; echo 44 45 # ----------------------------------------------------------------- 46 # Another array, "area2". 47 # Another way of assigning array variables... 48 # array_name=( XXX YYY ZZZ ... ) 49 50 area2=( zero one two three four ) 51 52 echo -n "area2[0] = " 53 echo ${area2[0]} 54 # Aha, zero-based indexing (first element of array is [0], not [1]). 55 56 echo -n "area2[1] = " 57 echo ${area2[1]} # [1] is second element of array. 58 # ----------------------------------------------------------------- 59 60 echo; echo; echo 61 62 # ----------------------------------------------- 63 # Yet another array, "area3". 64 # Yet another way of assigning array variables... 65 # array_name=([xx]=XXX [yy]=YYY ...) 66 67 area3=([17]=seventeen [24]=twenty-four) 68 69 echo -n "area3[17] = " 70 echo ${area3[17]} 71 72 echo -n "area3[24] = " 73 echo ${area3[24]} 74 # ----------------------------------------------- 75 76 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ARRAYINIT0"></A></P><P>As we have seen, a convenient way of initializing an entire array is the <TTCLASS="VARNAME">array=( element1 element2 ... elementN)</TT> notation.</P><P><ANAME="ARRAYOPSVARS"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN17323"></A><P>Bash permits array operations on variables, even if the variables are not explicitly declared as arrays.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 string=abcABC123ABCabc 2 echo ${string[@]} # abcABC123ABCabc 3 echo ${string[*]} # abcABC123ABCabc 4 echo ${string[0]} # abcABC123ABCabc 5 echo ${string[1]} # No output! 6 # Why? 7 echo ${#string[@]} # 1 8 # One element in the array. 9 # The string itself. 10 11 # Thank you, Michael Zick, for pointing this out.</PRE></TD></TR></TABLE> Once again this demonstrates that <AHREF="untyped.html#BVUNTYPED">Bash variables are untyped</A>. </P></DIV></TD></TR></TABLE><DIVCLASS="EXAMPLE"><HR><ANAME="POEM"></A><P><B>Example 26-2. Formatting a poem</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # poem.sh: Pretty-prints one of the document author's favorite poems. 3 4 # Lines of the poem (single stanza). 5 Line[1]="I do not know which to prefer," 6 Line[2]="The beauty of inflections" 7 Line[3]="Or the beauty of innuendoes," 8 Line[4]="The blackbird whistling" 9 Line[5]="Or just after." 10 11 # Attribution. 12 Attrib[1]=" Wallace Stevens" 13 Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\"" 14 # This poem is in the Public Domain (copyright expired). 15 16 echo 17 18 for index in 1 2 3 4 5 # Five lines. 19 do 20 printf " %s\n" "${Line[index]}" 21 done 22 23 for index in 1 2 # Two attribution lines. 24 do 25 printf " %s\n" "${Attrib[index]}" 26 done 27 28 echo 29 30 exit 0 31 32 # Exercise: 33 # -------- 34 # Modify this script to pretty-print a poem from a text data file.</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ARRAYSYNTAX"></A></P><P>Array variables have a syntax all their own, and even standard Bash commands and operators have special options adapted for array use.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARRAYOPS"></A><P><B>Example 26-3. Various array operations</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # array-ops.sh: More fun with arrays. 3 4 5 array=( zero one two three four five ) 6 # Element 0 1 2 3 4 5 7 8 echo ${array[0]} # zero 9 echo ${array:0} # zero 10 # Parameter expansion of first element, 11 #+ starting at position # 0 (1st character). 12 echo ${array:1} # ero 13 # Parameter expansion of first element, 14 #+ starting at position # 1 (2nd character). 15 16 echo "--------------" 17 18 echo ${#array[0]} # 4 19 # Length of first element of array. 20 echo ${#array} # 4 21 # Length of first element of array. 22 # (Alternate notation) 23 24 echo ${#array[1]} # 3 25 # Length of second element of array. 26 # Arrays in Bash have zero-based indexing. 27 28 echo ${#array[*]} # 6 29 # Number of elements in array. 30 echo ${#array[@]} # 6 31 # Number of elements in array. 32 33 echo "--------------" 34 35 array2=( [0]="first element" [1]="second element" [3]="fourth element" ) 36 37 echo ${array2[0]} # first element 38 echo ${array2[1]} # second element 39 echo ${array2[2]} # 40 # Skipped in initialization, and therefore null. 41 echo ${array2[3]} # fourth element 42 43 44 exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="ARRAYSTRINGOPS"></A></P><P>Many of the standard <AHREF="string-manipulation.html#STRINGMANIP">string operations</A> work on arrays.</P><DIVCLASS="EXAMPLE"><HR><ANAME="ARRAYSTROPS"></A><P><B>Example 26-4. String operations on arrays</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 # array-strops.sh: String operations on arrays. 3 # Script by Michael Zick. 4 # Used with permission. 5 6 # In general, any string operation in the ${name ... } notation 7 #+ can be applied to all string elements in an array 8 #+ with the ${name[@] ... } or ${name[*] ...} notation. 9 10 11 arrayZ=( one two three four five five ) 12 13 echo 14 15 # Trailing Substring Extraction 16 echo ${arrayZ[@]:0} # one two three four five five
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -