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

📄 bashver3.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Bash, version 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="Bash, versions 2 and 3"HREF="bash2.html"><LINKREL="PREVIOUS"TITLE="Bash, versions 2 and 3"HREF="bash2.html"><LINKREL="NEXT"TITLE="Endnotes"HREF="endnotes.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="SECT1"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="bash2.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 34. Bash, versions 2 and 3</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="endnotes.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASHVER3"></A>34.2. Bash, version 3</H1><P><ANAME="BASH3REF"></A></P><P>On July 27, 2004, Chet Ramey released version 3 of Bash.        This update fixed quite a number of bugs and added new        features.</P><P>Some of the more important added features:      <UL><LI><P><ANAME="BRACEEXPREF3"></A></P><P>A new, more generalized <BCLASS="COMMAND">{a..z}</B> <AHREF="special-chars.html#BRACEEXPREF">brace expansion</A> operator.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;for i in {1..10}   4&nbsp;#  Simpler and more straightforward than   5&nbsp;#+ for i in $(seq 10)   6&nbsp;do   7&nbsp;  echo -n "$i "   8&nbsp;done   9&nbsp;  10&nbsp;echo  11&nbsp;  12&nbsp;# 1 2 3 4 5 6 7 8 9 10  13&nbsp;  14&nbsp;  15&nbsp;  16&nbsp;# Or just . . .  17&nbsp;  18&nbsp;echo {a..z}    #  a b c d e f g h i j k l m n o p q r s t u v w x y z  19&nbsp;echo {z..a}    #  z y x w v u t s r q p o n m l k j i h g f e d c b a  20&nbsp;               #  Works backwards, too.  21&nbsp;echo {3..-2}   #  3 2 1 0 -1 -2  22&nbsp;echo {X..d}    #  X Y Z [  ] ^ _ ` a b c d  23&nbsp;               #  Shows (some of) the ASCII characters between Z and a,  24&nbsp;               #+ but don't rely on this type of behavior because . . .  25&nbsp;echo {]..a}    #  {]..a}  26&nbsp;               #  Why?</PRE></TD></TR></TABLE></P></LI><LI><P>The <BCLASS="COMMAND">${!array[@]}</B> operator, which	    expands to all the indices of a given <AHREF="arrays.html#ARRAYREF">array</A>.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;Array=(element-zero element-one element-two element-three)   4&nbsp;   5&nbsp;echo ${Array[0]}   # element-zero   6&nbsp;                   # First element of array.   7&nbsp;   8&nbsp;echo ${!Array[@]}  # 0 1 2 3   9&nbsp;                   # All the indices of Array.  10&nbsp;  11&nbsp;for i in ${!Array[@]}  12&nbsp;do  13&nbsp;  echo ${Array[i]} # element-zero  14&nbsp;                   # element-one  15&nbsp;                   # element-two  16&nbsp;                   # element-three  17&nbsp;                   #  18&nbsp;                   # All the elements in Array.  19&nbsp;done</PRE></TD></TR></TABLE></P></LI><LI><P><ANAME="REGEXMATCHREF"></A></P><P>The <BCLASS="COMMAND">=~</B> <AHREF="regexp.html#REGEXREF">Regular	    Expression</A> matching operator within a <AHREF="tests.html#DBLBRACKETS">double brackets</A> test expression.	    (Perl has a similar operator.)</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;variable="This is a fine mess."   4&nbsp;   5&nbsp;echo "$variable"   6&nbsp;   7&nbsp;# Regex matching with =~ operator within [[ double brackets ]].   8&nbsp;if [[ "$variable" =~ "T.........fin*es*" ]]   9&nbsp;#                    ^                 ^  10&nbsp;# NOTE: Quoting not necessary, as of version 3.2 of Bash.  11&nbsp;then  12&nbsp;  echo "match found"  13&nbsp;      # match found  14&nbsp;fi</PRE></TD></TR></TABLE></P><P>Or, more usefully:</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;input=$1   4&nbsp;   5&nbsp;   6&nbsp;if [[ "$input" =~ "[1-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]" ]]   7&nbsp;#                 ^ NOTE: Quoting not necessary, as of version 3.2 of Bash.   8&nbsp;# NNN-NN-NNNN (where each N is a digit). Initial digit must not be 0.   9&nbsp;then  10&nbsp;  echo "Social Security number."  11&nbsp;  # Process SSN.  12&nbsp;else  13&nbsp;  echo "Not a Social Security number!"  14&nbsp;  # Or, ask for corrected input.  15&nbsp;fi</PRE></TD></TR></TABLE></P><P>For additional examples of using the                <BCLASS="COMMAND">=~</B> operator, see <AHREF="contributed-scripts.html#WHX">Example A-31</A>,                <AHREF="here-docs.html#MAILBOXGREP">Example 18-14</A>, <AHREF="contributed-scripts.html#FINDSPLIT">Example A-37</A>, and <AHREF="contributed-scripts.html#TOHTML">Example A-26</A>.</P></LI><LI><P><ANAME="PIPEFAILREF"></A></P><P>The new <TTCLASS="OPTION">set -o pipefail</TT> option is	    useful for debugging <AHREF="special-chars.html#PIPEREF">pipes</A>. If	    this option is set, then the <AHREF="exit-status.html#EXITSTATUSREF">exit status</A> of a pipe	    is the exit status of the last command in the pipe to	    <SPANCLASS="emphasis"><ICLASS="EMPHASIS">fail</I></SPAN> (return a non-zero value), rather	    than the actual final command in the pipe.</P><P>See <AHREF="communications.html#FC4UPD">Example 15-42</A>.</P></LI></UL>      </P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The update to version 3 of Bash breaks a few scripts        that worked under earlier versions. <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Test critical legacy	scripts to make sure they still work!</I></SPAN></P><P>As it happens, a couple of the scripts in the        <SPANCLASS="emphasis"><ICLASS="EMPHASIS">Advanced Bash Scripting Guide</I></SPAN> had to be        fixed up (see <AHREF="contributed-scripts.html#OBJORIENTED">Example A-21</A>        and <AHREF="variables2.html#TOUT">Example 9-4</A>, for instance).</P></TD></TR></TABLE></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN19311"></A>34.2.1. Bash, version 3.1</H2><P>The version 3.1 update of Bash introduces a number of bugfixes	   and a few minor changes.</P><UL><LI><P>The <SPANCLASS="TOKEN">+=</SPAN> operator is now permitted in	        in places where previously only the <SPANCLASS="TOKEN">=</SPAN>		assignment operator was recognized.</P><P><ANAME="PLUSEQSTR"></A></P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;a=1   2&nbsp;echo $a        # 1   3&nbsp;   4&nbsp;a+=5           # Won't work under versions of Bash earlier than 3.1.   5&nbsp;echo $a        # 15   6&nbsp;   7&nbsp;a+=Hello   8&nbsp;echo $a        # 15Hello</PRE></TD></TR></TABLE>	      </P><P>Here, <SPANCLASS="TOKEN">+=</SPAN> functions as a <ICLASS="FIRSTTERM">string		concatenation</I> operator. Note that its behavior		in this particular context is different than within a		<AHREF="internal.html#LETREF">let</A> construct.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;a=1   2&nbsp;echo $a        # 1   3&nbsp;   4&nbsp;let a+=5       # Integer arithmetic, rather than string concatenation.   5&nbsp;echo $a        # 6   6&nbsp;   7&nbsp;let a+=Hello   # Doesn't "add" anything to a.   8&nbsp;echo $a        # 6</PRE></TD></TR></TABLE>	      </P></LI></UL></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN19329"></A>34.2.2. Bash, version 3.2</H2><P>This is pretty much a bugfix update.</P><UL><LI><P>In <AHREF="parameter-substitution.html#PSGLOB"><ICLASS="FIRSTTERM">global</I>	        parameter substitutions</A>, the pattern no longer anchors		at the start of the string.</P></LI><LI><P>The <TTCLASS="OPTION">--wordexp</TT> option disables	        <AHREF="process-sub.html#PROCESSSUBREF">process substitution</A>.</P></LI><LI><P>The <BCLASS="COMMAND">=~</B> <AHREF="bashver3.html#REGEXMATCHREF">Regular Expression                match operator</A> no longer requires                <AHREF="quoting.html#QUOTINGREF">quoting</A> of the                <ICLASS="FIRSTTERM">pattern</I> within <AHREF="tests.html#DBLBRACKETS">[[ ... ]]</A>.</P><DIVCLASS="CAUTION"><TABLECLASS="CAUTION"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/caution.png"HSPACE="5"ALT="Caution"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>In fact, quoting in this context is		<SPANCLASS="emphasis"><ICLASS="EMPHASIS">not</I></SPAN> advisable as it may		cause <ICLASS="FIRSTTERM">regex</I> evaluation to fail.	        See the <AHREF="https://bugs.launchpad.net/ubuntu-website/+bug/109931"TARGET="_top">		Ubuntu Bug List</A>		and <AHREF="http://en.wikinerds.org/index.php/Bash_syntax_and_semantics"TARGET="_top">		Wikinerds on Bash syntax</A>.</P><P>With Bash version 3.2.25(1), running on Fedora Core,		quoting works, but do not assume this will be the case on		<SPANCLASS="emphasis"><ICLASS="EMPHASIS">your</I></SPAN> machine.</P></TD></TR></TABLE></DIV></LI></UL></DIV></DIV><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="bash2.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="endnotes.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Bash, versions 2 and 3</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="bash2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Endnotes</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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