📄 moreadv.html
字号:
34 /g' | \ 35 # Filter out periods and commas, and 36 #+ change space between words to linefeed, 37 sort | uniq -c | sort -nr 38 # Finally prefix occurrence count and sort numerically. 39 ######################################################## 40 41 # This does the same job as the "wf.sh" example, 42 #+ but a bit more ponderously, and it runs more slowly (why?). 43 44 exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><ANAME="EXPRREF"></A><TTCLASS="USERINPUT"><B>expr</B></TT></DT><DD><P>All-purpose expression evaluator: Concatenates and evaluates the arguments according to the operation given (arguments must be separated by spaces). Operations may be arithmetic, comparison, string, or logical.</P><DIVCLASS="VARIABLELIST"><DL><DT><TTCLASS="USERINPUT"><B>expr 3 + 5</B></TT></DT><DD><P>returns <TTCLASS="LITERAL">8</TT></P></DD><DT><TTCLASS="USERINPUT"><B>expr 5 % 3</B></TT></DT><DD><P>returns 2</P></DD><DT><TTCLASS="USERINPUT"><B>expr 1 / 0</B></TT></DT><DD><P>returns the error message, <SPANCLASS="ERRORCODE">expr: division by zero</SPAN></P><P>Illegal arithmetic operations not allowed.</P></DD><DT><TTCLASS="USERINPUT"><B>expr 5 \* 3</B></TT></DT><DD><P>returns 15</P><P>The multiplication operator must be escaped when used in an arithmetic expression with <BCLASS="COMMAND">expr</B>.</P></DD><DT><TTCLASS="USERINPUT"><B>y=`expr $y + 1`</B></TT></DT><DD><P>Increment a variable, with the same effect as <TTCLASS="USERINPUT"><B>let y=y+1</B></TT> and <TTCLASS="USERINPUT"><B>y=$(($y+1))</B></TT>. This is an example of <AHREF="arithexp.html#ARITHEXPREF">arithmetic expansion</A>.</P></DD><DT><ANAME="EXPEXTRSUB"></A><TTCLASS="USERINPUT"><B>z=`expr substr $string $position $length`</B></TT></DT><DD><P>Extract substring of $length characters, starting at $position.</P></DD></DL></DIV><DIVCLASS="EXAMPLE"><HR><ANAME="EX45"></A><P><B>Example 15-9. Using <ICLASS="FIRSTTERM">expr</I></B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 # Demonstrating some of the uses of 'expr' 4 # ======================================= 5 6 echo 7 8 # Arithmetic Operators 9 # ---------- --------- 10 11 echo "Arithmetic Operators" 12 echo 13 a=`expr 5 + 3` 14 echo "5 + 3 = $a" 15 16 a=`expr $a + 1` 17 echo 18 echo "a + 1 = $a" 19 echo "(incrementing a variable)" 20 21 a=`expr 5 % 3` 22 # modulo 23 echo 24 echo "5 mod 3 = $a" 25 26 echo 27 echo 28 29 # Logical Operators 30 # ------- --------- 31 32 # Returns 1 if true, 0 if false, 33 #+ opposite of normal Bash convention. 34 35 echo "Logical Operators" 36 echo 37 38 x=24 39 y=25 40 b=`expr $x = $y` # Test equality. 41 echo "b = $b" # 0 ( $x -ne $y ) 42 echo 43 44 a=3 45 b=`expr $a \> 10` 46 echo 'b=`expr $a \> 10`, therefore...' 47 echo "If a > 10, b = 0 (false)" 48 echo "b = $b" # 0 ( 3 ! -gt 10 ) 49 echo 50 51 b=`expr $a \< 10` 52 echo "If a < 10, b = 1 (true)" 53 echo "b = $b" # 1 ( 3 -lt 10 ) 54 echo 55 # Note escaping of operators. 56 57 b=`expr $a \<= 3` 58 echo "If a <= 3, b = 1 (true)" 59 echo "b = $b" # 1 ( 3 -le 3 ) 60 # There is also a "\>=" operator (greater than or equal to). 61 62 63 echo 64 echo 65 66 67 68 # String Operators 69 # ------ --------- 70 71 echo "String Operators" 72 echo 73 74 a=1234zipper43231 75 echo "The string being operated upon is \"$a\"." 76 77 # length: length of string 78 b=`expr length $a` 79 echo "Length of \"$a\" is $b." 80 81 # index: position of first character in substring 82 # that matches a character in string 83 b=`expr index $a 23` 84 echo "Numerical position of first \"2\" in \"$a\" is \"$b\"." 85 86 # substr: extract substring, starting position & length specified 87 b=`expr substr $a 2 6` 88 echo "Substring of \"$a\", starting at position 2,\ 89 and 6 chars long is \"$b\"." 90 91 92 # The default behavior of the 'match' operations is to 93 #+ search for the specified match at the ***beginning*** of the string. 94 # 95 # uses Regular Expressions 96 b=`expr match "$a" '[0-9]*'` # Numerical count. 97 echo Number of digits at the beginning of \"$a\" is $b. 98 b=`expr match "$a" '\([0-9]*\)'` # Note that escaped parentheses 99 # == == + trigger substring match. 100 echo "The digits at the beginning of \"$a\" are \"$b\"." 101 102 echo 103 104 exit 0</PRE></TD></TR></TABLE><HR></DIV><DIVCLASS="IMPORTANT"><TABLECLASS="IMPORTANT"WIDTH="90%"BORDER="0"><TR><TDWIDTH="25"ALIGN="CENTER"VALIGN="TOP"><IMGSRC="common/important.png"HSPACE="5"ALT="Important"></TD><TDALIGN="LEFT"VALIGN="TOP"><P>The <AHREF="special-chars.html#NULLREF">:</A> operator can substitute for <BCLASS="COMMAND">match</B>. For example, <TTCLASS="USERINPUT"><B>b=`expr $a : [0-9]*`</B></TT> is the exact equivalent of <TTCLASS="USERINPUT"><B>b=`expr match $a [0-9]*`</B></TT> in the above listing.</P><P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 #!/bin/bash 2 3 echo 4 echo "String operations using \"expr \$string : \" construct" 5 echo "===================================================" 6 echo 7 8 a=1234zipper5FLIPPER43231 9 10 echo "The string being operated upon is \"`expr "$a" : '\(.*\)'`\"." 11 # Escaped parentheses grouping operator. == == 12 13 # *************************** 14 #+ Escaped parentheses 15 #+ match a substring 16 # *************************** 17 18 19 # If no escaped parentheses... 20 #+ then 'expr' converts the string operand to an integer. 21 22 echo "Length of \"$a\" is `expr "$a" : '.*'`." # Length of string 23 24 echo "Number of digits at the beginning of \"$a\" is `expr "$a" : '[0-9]*'`." 25 26 # ------------------------------------------------------------------------- # 27 28 echo 29 30 echo "The digits at the beginning of \"$a\" are `expr "$a" : '\([0-9]*\)'`." 31 # == == 32 echo "The first 7 characters of \"$a\" are `expr "$a" : '\(.......\)'`." 33 # ===== == == 34 # Again, escaped parentheses force a substring match. 35 # 36 echo "The last 7 characters of \"$a\" are `expr "$a" : '.*\(.......\)'`." 37 # ==== end of string operator ^^ 38 # (actually means skip over one or more of any characters until specified 39 #+ substring) 40 41 echo 42 43 exit 0</PRE></TD></TR></TABLE></P></TD></TR></TABLE></DIV></DD></DL></DIV><P>The above script illustrates how <BCLASS="COMMAND">expr</B> uses the <ICLASS="FIRSTTERM">escaped parentheses -- \( ... \) --</I> grouping operator in tandem with <AHREF="regexp.html#REGEXREF">regular expression</A> parsing to match a substring. Here is a another example, this time from <SPANCLASS="QUOTE">"real life."</SPAN> <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> 1 # Strip the whitespace from the beginning and end. 2 LRFDATE=`expr "$LRFDATE" : '[[:space:]]*\(.*\)[[:space:]]*$'` 3 4 # From Peter Knowles' "booklistgen.sh" script 5 #+ for converting files to Sony Librie/PRS-50X format. 6 # (http://booklistgensh.peterknowles.com)</PRE></TD></TR></TABLE> </P><P><AHREF="wrapper.html#PERLREF">Perl</A>, <AHREF="sedawk.html#SEDREF">sed</A>, and <AHREF="awk.html#AWKREF">awk</A> have far superior string parsing facilities. A short <BCLASS="COMMAND">sed</B> or <BCLASS="COMMAND">awk</B> <SPANCLASS="QUOTE">"subroutine"</SPAN> within a script (see <AHREF="wrapper.html">Section 33.3</A>) is an attractive alternative to <BCLASS="COMMAND">expr</B>.</P><P>See <AHREF="string-manipulation.html">Section 9.2</A> for more on using <BCLASS="COMMAND">expr</B> in string operations.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN9532"HREF="moreadv.html#AEN9532">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>And even when <ICLASS="FIRSTTERM">xargs</I> is not strictly necessary, it can speed up execution of a command involving <AHREF="timedate.html#BATCHPROCREF">batch-processing</A> of multiple files.</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="external.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="timedate.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">External Filters, Programs and Commands</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="external.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Time / Date Commands</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -