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

📄 parameter-substitution.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 3 页
字号:
  12&nbsp;  13&nbsp;case $# in  14&nbsp;  0|1)             # The vertical bar means "or" in this context.  15&nbsp;  echo "Usage: `basename $0` old_file_suffix new_file_suffix"  16&nbsp;  exit $E_BADARGS  # If 0 or 1 arg, then bail out.  17&nbsp;  ;;  18&nbsp;esac  19&nbsp;  20&nbsp;  21&nbsp;for filename in *.$1  22&nbsp;# Traverse list of files ending with 1st argument.  23&nbsp;do  24&nbsp;  mv $filename ${filename%$1}$2  25&nbsp;  #  Strip off part of filename matching 1st argument,  26&nbsp;  #+ then append 2nd argument.  27&nbsp;done  28&nbsp;  29&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="EXPREPL1"></A>Variable expansion / Substring	      replacement</B></P><DL><DT></DT><DD><P>These constructs have been adopted from		  <ICLASS="FIRSTTERM">ksh</I>.</P></DD><DT><TTCLASS="USERINPUT"><B>${var:pos}</B></TT></DT><DD><P>Variable <TTCLASS="REPLACEABLE"><I>var</I></TT> expanded,		    starting from offset <TTCLASS="REPLACEABLE"><I>pos</I></TT>.		  </P></DD><DT><TTCLASS="USERINPUT"><B>${var:pos:len}</B></TT></DT><DD><P>Expansion to a max of <TTCLASS="REPLACEABLE"><I>len</I></TT>		    characters of variable <TTCLASS="REPLACEABLE"><I>var</I></TT>, from offset		    <TTCLASS="REPLACEABLE"><I>pos</I></TT>. See <AHREF="contributed-scripts.html#PW">Example A-14</A>		    for an example of the creative use of this operator.		  </P></DD><DT><TTCLASS="USERINPUT"><B>${var/Pattern/Replacement}</B></TT></DT><DD><P>First match of <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>,		    within <TTCLASS="REPLACEABLE"><I>var</I></TT> replaced with		    <TTCLASS="REPLACEABLE"><I>Replacement</I></TT>.</P><P>If <TTCLASS="REPLACEABLE"><I>Replacement</I></TT> is		    omitted, then the first match of		    <TTCLASS="REPLACEABLE"><I>Pattern</I></TT> is replaced by		    <SPANCLASS="emphasis"><ICLASS="EMPHASIS">nothing</I></SPAN>, that is, deleted.</P></DD><DT><TTCLASS="USERINPUT"><B>${var//Pattern/Replacement}</B></TT></DT><DD><DIVCLASS="FORMALPARA"><P><B>Global replacement. </B><ANAME="PSGLOB"></A>		    All matches of <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>,		    within <TTCLASS="REPLACEABLE"><I>var</I></TT> replaced with		    <TTCLASS="REPLACEABLE"><I>Replacement</I></TT>.</P></DIV><P>As above, if <TTCLASS="REPLACEABLE"><I>Replacement</I></TT>		    is omitted, then all occurrences of		    <TTCLASS="REPLACEABLE"><I>Pattern</I></TT> are replaced by		    <SPANCLASS="emphasis"><ICLASS="EMPHASIS">nothing</I></SPAN>, that is, deleted.</P><DIVCLASS="EXAMPLE"><HR><ANAME="EX7"></A><P><B>Example 9-21. Using pattern matching to parse arbitrary strings</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;   3&nbsp;var1=abcd-1234-defg   4&nbsp;echo "var1 = $var1"   5&nbsp;   6&nbsp;t=${var1#*-*}   7&nbsp;echo "var1 (with everything, up to and including first - stripped out) = $t"   8&nbsp;#  t=${var1#*-}  works just the same,   9&nbsp;#+ since # matches the shortest string,  10&nbsp;#+ and * matches everything preceding, including an empty string.  11&nbsp;# (Thanks, Stephane Chazelas, for pointing this out.)  12&nbsp;  13&nbsp;t=${var1##*-*}  14&nbsp;echo "If var1 contains a \"-\", returns empty string...   var1 = $t"  15&nbsp;  16&nbsp;  17&nbsp;t=${var1%*-*}  18&nbsp;echo "var1 (with everything from the last - on stripped out) = $t"  19&nbsp;  20&nbsp;echo  21&nbsp;  22&nbsp;# -------------------------------------------  23&nbsp;path_name=/home/bozo/ideas/thoughts.for.today  24&nbsp;# -------------------------------------------  25&nbsp;echo "path_name = $path_name"  26&nbsp;t=${path_name##/*/}  27&nbsp;echo "path_name, stripped of prefixes = $t"  28&nbsp;# Same effect as   t=`basename $path_name` in this particular case.  29&nbsp;#  t=${path_name%/}; t=${t##*/}   is a more general solution,  30&nbsp;#+ but still fails sometimes.  31&nbsp;#  If $path_name ends with a newline, then `basename $path_name` will not work,  32&nbsp;#+ but the above expression will.  33&nbsp;# (Thanks, S.C.)  34&nbsp;  35&nbsp;t=${path_name%/*.*}  36&nbsp;# Same effect as   t=`dirname $path_name`  37&nbsp;echo "path_name, stripped of suffixes = $t"  38&nbsp;# These will fail in some cases, such as "../", "/foo////", # "foo/", "/".  39&nbsp;#  Removing suffixes, especially when the basename has no suffix,  40&nbsp;#+ but the dirname does, also complicates matters.  41&nbsp;# (Thanks, S.C.)  42&nbsp;  43&nbsp;echo  44&nbsp;  45&nbsp;t=${path_name:11}  46&nbsp;echo "$path_name, with first 11 chars stripped off = $t"  47&nbsp;t=${path_name:11:5}  48&nbsp;echo "$path_name, with first 11 chars stripped off, length 5 = $t"  49&nbsp;  50&nbsp;echo  51&nbsp;  52&nbsp;t=${path_name/bozo/clown}  53&nbsp;echo "$path_name with \"bozo\" replaced  by \"clown\" = $t"  54&nbsp;t=${path_name/today/}  55&nbsp;echo "$path_name with \"today\" deleted = $t"  56&nbsp;t=${path_name//o/O}  57&nbsp;echo "$path_name with all o's capitalized = $t"  58&nbsp;t=${path_name//o/}  59&nbsp;echo "$path_name with all o's deleted = $t"  60&nbsp;  61&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><TTCLASS="USERINPUT"><B>${var/#Pattern/Replacement}</B></TT></DT><DD><P>If <ICLASS="FIRSTTERM">prefix</I> of		  <TTCLASS="REPLACEABLE"><I>var</I></TT> matches		  <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>, then substitute		  <TTCLASS="REPLACEABLE"><I>Replacement</I></TT> for		  <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>.</P></DD><DT><TTCLASS="USERINPUT"><B>${var/%Pattern/Replacement}</B></TT></DT><DD><P>If <ICLASS="FIRSTTERM">suffix</I> of		  <TTCLASS="REPLACEABLE"><I>var</I></TT> matches		  <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>, then substitute		  <TTCLASS="REPLACEABLE"><I>Replacement</I></TT> for		  <TTCLASS="REPLACEABLE"><I>Pattern</I></TT>.</P><DIVCLASS="EXAMPLE"><HR><ANAME="VARMATCH"></A><P><B>Example 9-22. Matching patterns at prefix or suffix of string</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# var-match.sh:   3&nbsp;# Demo of pattern replacement at prefix / suffix of string.   4&nbsp;   5&nbsp;v0=abc1234zip1234abc    # Original variable.   6&nbsp;echo "v0 = $v0"         # abc1234zip1234abc   7&nbsp;echo   8&nbsp;   9&nbsp;# Match at prefix (beginning) of string.  10&nbsp;v1=${v0/#abc/ABCDEF}    # abc1234zip1234abc  11&nbsp;                        # |-|  12&nbsp;echo "v1 = $v1"         # ABCDEF1234zip1234abc  13&nbsp;                        # |----|  14&nbsp;  15&nbsp;# Match at suffix (end) of string.  16&nbsp;v2=${v0/%abc/ABCDEF}    # abc1234zip123abc  17&nbsp;                        #              |-|  18&nbsp;echo "v2 = $v2"         # abc1234zip1234ABCDEF  19&nbsp;                        #               |----|  20&nbsp;  21&nbsp;echo  22&nbsp;  23&nbsp;#  ----------------------------------------------------  24&nbsp;#  Must match at beginning / end of string,  25&nbsp;#+ otherwise no replacement results.  26&nbsp;#  ----------------------------------------------------  27&nbsp;v3=${v0/#123/000}       # Matches, but not at beginning.  28&nbsp;echo "v3 = $v3"         # abc1234zip1234abc  29&nbsp;                        # NO REPLACEMENT.  30&nbsp;v4=${v0/%123/000}       # Matches, but not at end.  31&nbsp;echo "v4 = $v4"         # abc1234zip1234abc  32&nbsp;                        # NO REPLACEMENT.  33&nbsp;  34&nbsp;exit 0			</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="VARPREFIXM"></A><TTCLASS="USERINPUT"><B>${!varprefix*}</B></TT>, <TTCLASS="USERINPUT"><B>${!varprefix@}</B></TT></DT><DD><P>Matches <SPANCLASS="emphasis"><ICLASS="EMPHASIS">names</I></SPAN> of all		  previously declared variables beginning		    with <TTCLASS="PARAMETER"><I>varprefix</I></TT>.		      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;xyz23=whatever   2&nbsp;xyz24=   3&nbsp;   4&nbsp;a=${!xyz*}      # Expands to *names* of declared variables beginning with "xyz".   5&nbsp;echo "a = $a"   # a = xyz23 xyz24   6&nbsp;a=${!xyz@}      # Same as above.   7&nbsp;echo "a = $a"   # a = xyz23 xyz24   8&nbsp;   9&nbsp;# Bash, version 2.04, adds this feature.</PRE></TD></TR></TABLE>                  </P></DD></DL></DIV></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN5317"HREF="parameter-substitution.html#AEN5317">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>If $parameter is null in a		      non-interactive script, it will terminate with a <AHREF="exitcodes.html#EXITCODESREF"><SPANCLASS="RETURNVALUE">127</SPAN>		      exit status</A> (the Bash error code for		      <SPANCLASS="QUOTE">"command not found"</SPAN>).</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="string-manipulation.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="declareref.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Manipulating Strings</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="variables2.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Typing variables: <BCLASS="COMMAND">declare</B> or	  <BCLASS="COMMAND">typeset</B></TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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