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

📄 commandsub.html

📁 Shall高级编程
💻 HTML
📖 第 1 页 / 共 2 页
字号:
></TABLE><HR></DIV><P><ANAME="CSTOOLSET"></A></P><TABLECLASS="SIDEBAR"BORDER="1"CELLPADDING="5"><TR><TD><DIVCLASS="SIDEBAR"><ANAME="AEN6482"></A><P>Command substitution makes it possible to extend the		toolset available to Bash. It is simply a matter		of writing a program or script that outputs to		<TTCLASS="FILENAME">stdout</TT> (like a well-behaved UNIX		tool should) and assigning that output to a variable.</P><P>		<TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#include &#60;stdio.h&#62;   2&nbsp;   3&nbsp;/*  "Hello, world." C program  */		   4&nbsp;   5&nbsp;int main()   6&nbsp;{   7&nbsp;  printf( "Hello, world." );   8&nbsp;  return (0);   9&nbsp;}</PRE></TD></TR></TABLE>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>gcc -o hello hello.c</B></TT> 	      </PRE></TD></TR></TABLE>		</P><P>                <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# hello.sh		   3&nbsp;   4&nbsp;greeting=`./hello`   5&nbsp;echo $greeting</PRE></TD></TR></TABLE>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>sh hello.sh</B></TT> <TTCLASS="COMPUTEROUTPUT">Hello, world.</TT> 	        </PRE></TD></TR></TABLE>	        </P></DIV></TD></TR></TABLE><DIVCLASS="NOTE"><TABLECLASS="NOTE"WIDTH="100%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/note.png"HSPACE="5"ALT="Note"></TD><TDALIGN="LEFT"VALIGN="TOP"><P><ANAME="CSPARENS"></A>The <BCLASS="COMMAND">$(...)</B>		form has superseded backticks for command		substitution.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;output=$(sed -n /"$1"/p $file)   # From "grp.sh"	example.   2&nbsp;	         3&nbsp;# Setting a variable to the contents of a text file.   4&nbsp;File_contents1=$(cat $file1)         5&nbsp;File_contents2=$(&#60;$file2)        # Bash permits this also.</PRE></TD></TR></TABLE></P><P>The <BCLASS="COMMAND">$(...)</B> form of command substitution	        treats a double backslash in a different way than		<BCLASS="COMMAND">`...`</B>.</P><P>			      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo `echo \\`</B></TT> <TTCLASS="COMPUTEROUTPUT"></TT>  <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>echo $(echo \\)</B></TT> <TTCLASS="COMPUTEROUTPUT">\</TT> 	      </PRE></TD></TR></TABLE>              </P><P><ANAME="CSNEST"></A></P><P>The <BCLASS="COMMAND">$(...)</B> form of command                substitution permits nesting.		  <ANAME="AEN6517"HREF="#FTN.AEN6517">[3]</A>				</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;word_count=$( wc -w $(ls -l | awk '{print $9}') )</PRE></TD></TR></TABLE>              </P><P>Or, for something a bit more elaborate . . .</P><DIVCLASS="EXAMPLE"><HR><ANAME="AGRAM2"></A><P><B>Example 11-3. Finding anagrams</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# agram2.sh   3&nbsp;# Example of nested command substitution.   4&nbsp;   5&nbsp;#  Uses "anagram" utility   6&nbsp;#+ that is part of the author's "yawl" word list package.   7&nbsp;#  http://ibiblio.org/pub/Linux/libs/yawl-0.3.2.tar.gz   8&nbsp;#  http://personal.riverusers.com/~thegrendel/yawl-0.3.2.tar.gz   9&nbsp;  10&nbsp;E_NOARGS=66  11&nbsp;E_BADARG=67  12&nbsp;MINLEN=7  13&nbsp;  14&nbsp;if [ -z "$1" ]  15&nbsp;then  16&nbsp;  echo "Usage $0 LETTERSET"  17&nbsp;  exit $E_NOARGS         # Script needs a command-line argument.  18&nbsp;elif [ ${#1} -lt $MINLEN ]  19&nbsp;then  20&nbsp;  echo "Argument must have at least $MINLEN letters."  21&nbsp;  exit $E_BADARG  22&nbsp;fi  23&nbsp;  24&nbsp;  25&nbsp;  26&nbsp;FILTER='.......'         # Must have at least 7 letters.  27&nbsp;#       1234567  28&nbsp;Anagrams=( $(echo $(anagram $1 | grep $FILTER) ) )  29&nbsp;#          $(     $(  nested command sub.    ) )  30&nbsp;#        (              array assignment         )  31&nbsp;  32&nbsp;echo  33&nbsp;echo "${#Anagrams[*]}  7+ letter anagrams found"  34&nbsp;echo  35&nbsp;echo ${Anagrams[0]}      # First anagram.  36&nbsp;echo ${Anagrams[1]}      # Second anagram.  37&nbsp;                         # Etc.  38&nbsp;  39&nbsp;# echo "${Anagrams[*]}"  # To list all the anagrams in a single line . . .  40&nbsp;  41&nbsp;#  Look ahead to the "Arrays" chapter for enlightenment on  42&nbsp;#+ what's going on here.  43&nbsp;  44&nbsp;# See also the agram.sh script for an example of anagram finding.  45&nbsp;  46&nbsp;exit $?</PRE></TD></TR></TABLE><HR></DIV></TD></TR></TABLE></DIV><P>Examples of command substitution in shell scripts:       <OLTYPE="1"><LI><P><AHREF="loops.html#BINGREP">Example 10-7</A></P></LI><LI><P><AHREF="testbranch.html#CASECMD">Example 10-26</A></P></LI><LI><P><AHREF="randomvar.html#SEEDINGRANDOM">Example 9-31</A></P></LI><LI><P><AHREF="moreadv.html#EX57">Example 15-3</A></P></LI><LI><P><AHREF="textproc.html#LOWERCASE">Example 15-21</A></P></LI><LI><P><AHREF="textproc.html#GRP">Example 15-17</A></P></LI><LI><P><AHREF="extmisc.html#EX53">Example 15-52</A></P></LI><LI><P><AHREF="loops.html#EX24">Example 10-13</A></P></LI><LI><P><AHREF="loops.html#SYMLINKS">Example 10-10</A></P></LI><LI><P><AHREF="filearchiv.html#STRIPC">Example 15-31</A></P></LI><LI><P><AHREF="redircb.html#REDIR4">Example 19-8</A></P></LI><LI><P><AHREF="contributed-scripts.html#TREE">Example A-17</A></P></LI><LI><P><AHREF="procref1.html#PIDID">Example 27-2</A></P></LI><LI><P><AHREF="mathc.html#MONTHLYPMT">Example 15-45</A></P></LI><LI><P><AHREF="mathc.html#BASE">Example 15-46</A></P></LI><LI><P><AHREF="mathc.html#ALTBC">Example 15-47</A></P></LI></OL>     </P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN6415"HREF="commandsub.html#AEN6415">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>For purposes of <ICLASS="FIRSTTERM">command		substitution</I>, a <BCLASS="COMMAND">command</B>		may be an external system command, an internal scripting		<AHREF="internal.html#BUILTINREF">builtin</A>, or even <AHREF="assortedtips.html#RVT">a script function</A>.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN6421"HREF="commandsub.html#AEN6421">[2]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>In a more technically correct sense,		<ICLASS="FIRSTTERM">command substitution</I> extracts the		<TTCLASS="FILENAME">stdout</TT> of a command, then assigns		it to a variable using the <SPANCLASS="TOKEN">=</SPAN>		operator.</P></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN6517"HREF="commandsub.html#AEN6517">[3]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>		    In fact, nesting with backticks is also possible,		    but only by escaping the inner backticks, as John		    Default points out.		      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;word_count=` wc -w \`ls -l | awk '{print $9}'\` `</PRE></TD></TR></TABLE>		    </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="testbranch.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="arithexp.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Testing and Branching</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="part3.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Arithmetic Expansion</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

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