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

📄 bashver3.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 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.57"><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"><TABLEWIDTH="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">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">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="BASHVER3">34.2. Bash, version 3</A></H1><P><ANAME="BASH3REF"></A></P><P>On July 27, 2004, Chet Ramey released version 3 of Bash. This        update fixes quite a number of bug in Bash and adds some new	features.</P><P>Some of the added features are:      <UL><LI><P>A new, more generalized <BCLASS="COMMAND">{a..z}</B> <AHREF="special-chars.html#BRACEEXPREF">brace expansion</A> operator.               <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</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>.	      <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;if [[ "$variable" =~ "T*fin*es*" ]]   8&nbsp;# Regex matching with =~ operator within [[ double brackets ]].   9&nbsp;then  10&nbsp;  echo "match found"  11&nbsp;      # match found  12&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;# NNN-NN-NNNN   8&nbsp;# Where each N is a digit.   9&nbsp;# But, initial digit must not be 0.  10&nbsp;then  11&nbsp;  echo "Social Security number."  12&nbsp;  # Process SSN.  13&nbsp;else  14&nbsp;  echo "Not a Social Security number!"  15&nbsp;  # Or, ask for corrected input.  16&nbsp;fi</PRE></TD></TR></TABLE></P><P>For another example of using the <BCLASS="COMMAND">=~</B>	        operator, see <AHREF="contributed-scripts.html#WHX">Example A-28</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. <ICLASS="EMPHASIS">Test critical legacy	scripts to make sure they still work!</I></P><P>As it happens, a couple of the scripts in the        <ICLASS="EMPHASIS">Advanced Bash Scripting Guide</I> had to be        fixed up (see <AHREF="contributed-scripts.html#OBJORIENTED">Example A-20</A>        and <AHREF="variables2.html#TOUT">Example 9-4</A>, for instance).</P></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="bash2.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="endnotes.html">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">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 + -