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

📄 bash2.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Bash, versions 2 and 3</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="Shell Scripting Under Windows"HREF="winscript.html"><LINKREL="NEXT"TITLE="Bash, version 3"HREF="bashver3.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="winscript.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="bashver3.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="BASH2"></A>Chapter 34. Bash, versions 2 and 3</H1><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASHVER2"></A>34.1. Bash, version 2</H1><P><ANAME="BASH2REF"></A></P><P>The current version of <ICLASS="FIRSTTERM">Bash</I>, the one	you have running on your machine, is version 2.xx.y or 3.xx.y.	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $BASH_VERSION</B></TT> <TTCLASS="COMPUTEROUTPUT">3.2.25(1)-release</TT> 	      </PRE></TD></TR></TABLE>	The  version 2 update of the classic Bash scripting language added array	variables,	  <ANAME="AEN19239"HREF="#FTN.AEN19239">[1]</A>	string and parameter expansion, and a better method	of indirect variable references, among other features.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX77"></A><P><B>Example 34-1. String expansion</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# String expansion.   4&nbsp;# Introduced with version 2 of Bash.   5&nbsp;   6&nbsp;#  Strings of the form $'xxx'   7&nbsp;#+ have the standard escaped characters interpreted.    8&nbsp;   9&nbsp;echo $'Ringing bell 3 times \a \a \a'  10&nbsp;     # May only ring once with certain terminals.  11&nbsp;echo $'Three form feeds \f \f \f'  12&nbsp;echo $'10 newlines \n\n\n\n\n\n\n\n\n\n'  13&nbsp;echo $'\102\141\163\150'   # Bash  14&nbsp;                           # Octal equivalent of characters.  15&nbsp;  16&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><P><ANAME="VARREFNEW"></A></P><DIVCLASS="EXAMPLE"><HR><ANAME="EX78"></A><P><B>Example 34-2. Indirect variable references - the new way</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Indirect variable referencing.   4&nbsp;# This has a few of the attributes of references in C++.   5&nbsp;   6&nbsp;   7&nbsp;a=letter_of_alphabet   8&nbsp;letter_of_alphabet=z   9&nbsp;  10&nbsp;echo "a = $a"           # Direct reference.  11&nbsp;  12&nbsp;echo "Now a = ${!a}"    # Indirect reference.  13&nbsp;# The ${!variable} notation is greatly superior to the old "eval var1=\$$var2"  14&nbsp;  15&nbsp;echo  16&nbsp;  17&nbsp;t=table_cell_3  18&nbsp;table_cell_3=24  19&nbsp;echo "t = ${!t}"                      # t = 24  20&nbsp;table_cell_3=387  21&nbsp;echo "Value of t changed to ${!t}"    # 387  22&nbsp;  23&nbsp;#  This is useful for referencing members of an array or table,  24&nbsp;#+ or for simulating a multi-dimensional array.  25&nbsp;#  An indexing option (analogous to pointer arithmetic)  26&nbsp;#+ would have been nice. Sigh.  27&nbsp;  28&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="RESISTOR"></A><P><B>Example 34-3. Simple database application, using indirect variable	  referencing</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# resistor-inventory.sh   3&nbsp;# Simple database application using indirect variable referencing.   4&nbsp;   5&nbsp;# ============================================================== #   6&nbsp;# Data   7&nbsp;   8&nbsp;B1723_value=470                                   # Ohms   9&nbsp;B1723_powerdissip=.25                             # Watts  10&nbsp;B1723_colorcode="yellow-violet-brown"             # Color bands  11&nbsp;B1723_loc=173                                     # Where they are  12&nbsp;B1723_inventory=78                                # How many  13&nbsp;  14&nbsp;B1724_value=1000  15&nbsp;B1724_powerdissip=.25  16&nbsp;B1724_colorcode="brown-black-red"  17&nbsp;B1724_loc=24N  18&nbsp;B1724_inventory=243  19&nbsp;  20&nbsp;B1725_value=10000  21&nbsp;B1725_powerdissip=.25  22&nbsp;B1725_colorcode="brown-black-orange"  23&nbsp;B1725_loc=24N  24&nbsp;B1725_inventory=89  25&nbsp;  26&nbsp;# ============================================================== #  27&nbsp;  28&nbsp;  29&nbsp;echo  30&nbsp;  31&nbsp;PS3='Enter catalog number: '  32&nbsp;  33&nbsp;echo  34&nbsp;  35&nbsp;select catalog_number in "B1723" "B1724" "B1725"  36&nbsp;do  37&nbsp;  Inv=${catalog_number}_inventory  38&nbsp;  Val=${catalog_number}_value  39&nbsp;  Pdissip=${catalog_number}_powerdissip  40&nbsp;  Loc=${catalog_number}_loc  41&nbsp;  Ccode=${catalog_number}_colorcode  42&nbsp;  43&nbsp;  echo  44&nbsp;  echo "Catalog number $catalog_number:"  45&nbsp;  echo "There are ${!Inv} of [${!Val} ohm / ${!Pdissip} watt] resistors in stock."  46&nbsp;  echo "These are located in bin # ${!Loc}."  47&nbsp;  echo "Their color code is \"${!Ccode}\"."  48&nbsp;  49&nbsp;  break  50&nbsp;done  51&nbsp;  52&nbsp;echo; echo  53&nbsp;  54&nbsp;# Exercises:  55&nbsp;# ---------  56&nbsp;# 1) Rewrite this script to read its data from an external file.  57&nbsp;# 2) Rewrite this script to use arrays,  58&nbsp;#+   rather than indirect variable referencing.  59&nbsp;#    Which method is more straightforward and intuitive?  60&nbsp;  61&nbsp;  62&nbsp;# Notes:  63&nbsp;# -----  64&nbsp;#  Shell scripts are inappropriate for anything except the most simple  65&nbsp;#+ database applications, and even then it involves workarounds and kludges.  66&nbsp;#  Much better is to use a language with native support for data structures,  67&nbsp;#+ such as C++ or Java (or even Perl).  68&nbsp;  69&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX79"></A><P><B>Example 34-4. Using arrays and other miscellaneous trickery	  to deal four random hands from a deck of cards</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;# Cards:   4&nbsp;# Deals four random hands from a deck of cards.   5&nbsp;   6&nbsp;UNPICKED=0   7&nbsp;PICKED=1   8&nbsp;   9&nbsp;DUPE_CARD=99  10&nbsp;  11&nbsp;LOWER_LIMIT=0  12&nbsp;UPPER_LIMIT=51  13&nbsp;CARDS_IN_SUIT=13  14&nbsp;CARDS=52  15&nbsp;  16&nbsp;declare -a Deck  17&nbsp;declare -a Suits  18&nbsp;declare -a Cards  19&nbsp;#  It would have been easier to implement and more intuitive  20&nbsp;#+ with a single, 3-dimensional array.  21&nbsp;#  Perhaps a future version of Bash will support multidimensional arrays.  22&nbsp;  23&nbsp;  24&nbsp;initialize_Deck ()  25&nbsp;{  26&nbsp;i=$LOWER_LIMIT  27&nbsp;until [ "$i" -gt $UPPER_LIMIT ]  28&nbsp;do  29&nbsp;  Deck[i]=$UNPICKED   # Set each card of "Deck" as unpicked.  30&nbsp;  let "i += 1"  31&nbsp;done  32&nbsp;echo  33&nbsp;}  34&nbsp;  35&nbsp;initialize_Suits ()  36&nbsp;{  37&nbsp;Suits[0]=C #Clubs  38&nbsp;Suits[1]=D #Diamonds  39&nbsp;Suits[2]=H #Hearts  40&nbsp;Suits[3]=S #Spades  41&nbsp;}  42&nbsp;  43&nbsp;initialize_Cards ()  44&nbsp;{  45&nbsp;Cards=(2 3 4 5 6 7 8 9 10 J Q K A)  46&nbsp;# Alternate method of initializing an array.  47&nbsp;}  48&nbsp;  49&nbsp;pick_a_card ()  50&nbsp;{  51&nbsp;card_number=$RANDOM  52&nbsp;let "card_number %= $CARDS"  53&nbsp;if [ "${Deck[card_number]}" -eq $UNPICKED ]  54&nbsp;then  55&nbsp;  Deck[card_number]=$PICKED  56&nbsp;  return $card_number  57&nbsp;else    58&nbsp;  return $DUPE_CARD  59&nbsp;fi  60&nbsp;}  61&nbsp;  62&nbsp;parse_card ()  63&nbsp;{  64&nbsp;number=$1  65&nbsp;let "suit_number = number / CARDS_IN_SUIT"  66&nbsp;suit=${Suits[suit_number]}  67&nbsp;echo -n "$suit-"  68&nbsp;let "card_no = number % CARDS_IN_SUIT"  69&nbsp;Card=${Cards[card_no]}  70&nbsp;printf %-4s $Card  71&nbsp;# Print cards in neat columns.  72&nbsp;}  73&nbsp;  74&nbsp;seed_random ()  # Seed random number generator.  75&nbsp;{               # What happens if you don't do this?  76&nbsp;seed=`eval date +%s`  77&nbsp;let "seed %= 32766"  78&nbsp;RANDOM=$seed  79&nbsp;#  What are some other methods  80&nbsp;#+ of seeding the random number generator?  81&nbsp;}  82&nbsp;  83&nbsp;deal_cards ()  84&nbsp;{  85&nbsp;echo  86&nbsp;  87&nbsp;cards_picked=0  88&nbsp;while [ "$cards_picked" -le $UPPER_LIMIT ]  89&nbsp;do  90&nbsp;  pick_a_card  91&nbsp;  t=$?  92&nbsp;  93&nbsp;  if [ "$t" -ne $DUPE_CARD ]  94&nbsp;  then  95&nbsp;    parse_card $t  96&nbsp;  97&nbsp;    u=$cards_picked+1  98&nbsp;    # Change back to 1-based indexing (temporarily). Why?  99&nbsp;    let "u %= $CARDS_IN_SUIT" 100&nbsp;    if [ "$u" -eq 0 ]   # Nested if/then condition test. 101&nbsp;    then 102&nbsp;     echo 103&nbsp;     echo 104&nbsp;    fi 105&nbsp;    # Separate hands. 106&nbsp; 107&nbsp;    let "cards_picked += 1" 108&nbsp;  fi   109&nbsp;done   110&nbsp; 111&nbsp;echo 112&nbsp; 113&nbsp;return 0 114&nbsp;} 115&nbsp; 116&nbsp; 117&nbsp;# Structured programming: 118&nbsp;# Entire program logic modularized in functions. 119&nbsp; 120&nbsp;#================ 121&nbsp;seed_random 122&nbsp;initialize_Deck 123&nbsp;initialize_Suits 124&nbsp;initialize_Cards 125&nbsp;deal_cards 126&nbsp;#================ 127&nbsp; 128&nbsp;exit 0 129&nbsp; 130&nbsp; 131&nbsp; 132&nbsp;# Exercise 1: 133&nbsp;# Add comments to thoroughly document this script. 134&nbsp; 135&nbsp;# Exercise 2: 136&nbsp;# Add a routine (function) to print out each hand sorted in suits. 137&nbsp;# You may add other bells and whistles if you like. 138&nbsp; 139&nbsp;# Exercise 3: 140&nbsp;# Simplify and streamline the logic of the script.</PRE></TD></TR></TABLE><HR></DIV></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN19239"HREF="bash2.html#AEN19239">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>Chet Ramey has promised associative arrays	  (a nifty Perl feature) in a future Bash release. As of version	  3.2, this has not yet happened.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="winscript.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="bashver3.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Shell Scripting Under Windows</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part5.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Bash, version 3</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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