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

📄 arrays.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!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&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;#  Array members need not be consecutive or contiguous.   9&nbsp;  10&nbsp;#  Some members of the array can be left uninitialized.  11&nbsp;#  Gaps in the array are okay.  12&nbsp;#  In fact, arrays with sparse data ("sparse arrays")  13&nbsp;#+ are useful in spreadsheet-processing software.  14&nbsp;  15&nbsp;  16&nbsp;echo -n "area[11] = "  17&nbsp;echo ${area[11]}    #  {curly brackets} needed.  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;# Contents of uninitialized array variable print blank (null variable).  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;# Sum of two array variables assigned to third  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;# This fails because adding an integer to a string is not permitted.  42&nbsp;  43&nbsp;echo; echo; echo  44&nbsp;  45&nbsp;# -----------------------------------------------------------------  46&nbsp;# Another array, "area2".  47&nbsp;# Another way of assigning array variables...  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;# Aha, zero-based indexing (first element of array is [0], not [1]).  55&nbsp;  56&nbsp;echo -n "area2[1] = "  57&nbsp;echo ${area2[1]}    # [1] is second element of array.  58&nbsp;# -----------------------------------------------------------------  59&nbsp;  60&nbsp;echo; echo; echo  61&nbsp;  62&nbsp;# -----------------------------------------------  63&nbsp;# Yet another array, "area3".  64&nbsp;# Yet another way of assigning array variables...  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></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&nbsp;string=abcABC123ABCabc   2&nbsp;echo ${string[@]}               # abcABC123ABCabc   3&nbsp;echo ${string[*]}               # abcABC123ABCabc    4&nbsp;echo ${string[0]}               # abcABC123ABCabc   5&nbsp;echo ${string[1]}               # No output!   6&nbsp;                                # Why?   7&nbsp;echo ${#string[@]}              # 1   8&nbsp;                                # One element in the array.   9&nbsp;                                # The string itself.  10&nbsp;  11&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# poem.sh: Pretty-prints one of the document author's favorite poems.   3&nbsp;   4&nbsp;# Lines of the poem (single stanza).   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;# Attribution.  12&nbsp;Attrib[1]=" Wallace Stevens"  13&nbsp;Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\""  14&nbsp;# This poem is in the Public Domain (copyright expired).  15&nbsp;  16&nbsp;echo  17&nbsp;  18&nbsp;for index in 1 2 3 4 5    # Five lines.  19&nbsp;do  20&nbsp;  printf "     %s\n" "${Line[index]}"  21&nbsp;done  22&nbsp;  23&nbsp;for index in 1 2          # Two attribution lines.  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;# Exercise:  33&nbsp;# --------  34&nbsp;# 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&nbsp;#!/bin/bash   2&nbsp;# array-ops.sh: More fun with arrays.   3&nbsp;   4&nbsp;   5&nbsp;array=( zero one two three four five )   6&nbsp;# Element 0   1   2    3     4    5   7&nbsp;   8&nbsp;echo ${array[0]}       #  zero   9&nbsp;echo ${array:0}        #  zero  10&nbsp;                       #  Parameter expansion of first element,  11&nbsp;                       #+ starting at position # 0 (1st character).  12&nbsp;echo ${array:1}        #  ero  13&nbsp;                       #  Parameter expansion of first element,  14&nbsp;                       #+ starting at position # 1 (2nd character).  15&nbsp;  16&nbsp;echo "--------------"  17&nbsp;  18&nbsp;echo ${#array[0]}      #  4  19&nbsp;                       #  Length of first element of array.  20&nbsp;echo ${#array}         #  4  21&nbsp;                       #  Length of first element of array.  22&nbsp;                       #  (Alternate notation)  23&nbsp;  24&nbsp;echo ${#array[1]}      #  3  25&nbsp;                       #  Length of second element of array.  26&nbsp;                       #  Arrays in Bash have zero-based indexing.  27&nbsp;  28&nbsp;echo ${#array[*]}      #  6  29&nbsp;                       #  Number of elements in array.  30&nbsp;echo ${#array[@]}      #  6  31&nbsp;                       #  Number of elements in array.  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]}      # first element  38&nbsp;echo ${array2[1]}      # second element  39&nbsp;echo ${array2[2]}      #  40&nbsp;                       # Skipped in initialization, and therefore null.  41&nbsp;echo ${array2[3]}      # fourth element  42&nbsp;  43&nbsp;  44&nbsp;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&nbsp;#!/bin/bash   2&nbsp;# array-strops.sh: String operations on arrays.   3&nbsp;# Script by Michael Zick.   4&nbsp;# Used with permission.   5&nbsp;   6&nbsp;#  In general, any string operation in the ${name ... } notation   7&nbsp;#+ can be applied to all string elements in an array   8&nbsp;#+ with the ${name[@] ... } or ${name[*] ...} notation.   9&nbsp;  10&nbsp;  11&nbsp;arrayZ=( one two three four five five )  12&nbsp;  13&nbsp;echo  14&nbsp;  15&nbsp;# Trailing Substring Extraction  16&nbsp;echo ${arrayZ[@]:0}     # one two three four five five

⌨️ 快捷键说明

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