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

📄 operations.html

📁 一本完整的描述Unix Shell 编程的工具书的所有范例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Operations and Related Topics</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Basics"HREF="part2.html"><LINKREL="PREVIOUS"TITLE="Testing Your Knowledge of Tests"HREF="testtest.html"><LINKREL="NEXT"TITLE="Numerical Constants"HREF="numerical-constants.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="CHAPTER"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="testtest.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="numerical-constants.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="OPERATIONS">Chapter 8. Operations and Related Topics</A></H1><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="OPS">8.1. Operators</A></H1><DIVCLASS="VARIABLELIST"><P><B><ANAME="ASNOP1"></A>assignment</B></P><DL><DT><TTCLASS="REPLACEABLE"><I>variable assignment</I></TT></DT><DD><P>Initializing or changing the value of a variable</P></DD><DT>=</DT><DD><P>All-purpose assignment operator, which works for both	    arithmetic and string assignments.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;var=27   2&nbsp;category=minerals  # No spaces allowed after the "=".</PRE></TD></TR></TABLE>	    </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>Do not confuse the <SPANCLASS="QUOTE">"="</SPAN> assignment	      operator with the <AHREF="comparison-ops.html#EQUALSIGNREF">=</A> test	      operator.</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#    = as a test operator   2&nbsp;   3&nbsp;if [ "$string1" = "$string2" ]   4&nbsp;# if [ "X$string1" = "X$string2" ] is safer,   5&nbsp;# to prevent an error message should one of the variables be empty.   6&nbsp;# (The prepended "X" characters cancel out.)    7&nbsp;then   8&nbsp;   command   9&nbsp;fi</PRE></TD></TR></TABLE>	    </P></TD></TR></TABLE></DIV></DD></DL></DIV><DIVCLASS="VARIABLELIST"><P><B><ANAME="AROPS1"></A>arithmetic operators</B></P><DL><DT><SPANCLASS="TOKEN">+</SPAN></DT><DD><P>plus</P></DD><DT><SPANCLASS="TOKEN">-</SPAN></DT><DD><P>minus</P></DD><DT><SPANCLASS="TOKEN">*</SPAN></DT><DD><P>multiplication</P></DD><DT><SPANCLASS="TOKEN">/</SPAN></DT><DD><P>division</P></DD><DT><ANAME="EXPONENTIATIONREF"></A><SPANCLASS="TOKEN">**</SPAN></DT><DD><P>exponentiation	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;# Bash, version 2.02, introduced the "**" exponentiation operator.   2&nbsp;   3&nbsp;let "z=5**3"   4&nbsp;echo "z = $z"   # z = 125</PRE></TD></TR></TABLE>          </P></DD><DT><ANAME="MODULOREF"></A><SPANCLASS="TOKEN">%</SPAN></DT><DD><P>modulo, or mod (returns the	    <ICLASS="FIRSTTERM">remainder</I> of an integer division	    operation)</P><P>	      <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="SCREEN"> <TTCLASS="PROMPT">bash$ </TT><TTCLASS="USERINPUT"><B>expr 5 % 3</B></TT> <TTCLASS="COMPUTEROUTPUT">2</TT> 	      </PRE></TD></TR></TABLE>	      <ICLASS="EMPHASIS">5/3 = 1 with remainder 2</I>	    </P><P>This operator finds use in, among other things,	    generating numbers within a specific range (see <AHREF="randomvar.html#EX21">Example 9-24</A> and <AHREF="randomvar.html#RANDOMTEST">Example 9-27</A>) and	    formatting program output (see <AHREF="arrays.html#QFUNCTION">Example 26-15</A> and	    <AHREF="contributed-scripts.html#COLLATZ">Example A-6</A>). It can even be used to generate	    prime numbers, (see <AHREF="contributed-scripts.html#PRIMES">Example A-16</A>). Modulo turns	    up surprisingly often in various numerical recipes.</P><DIVCLASS="EXAMPLE"><HR><ANAME="GCD"></A><P><B>Example 8-1. Greatest common divisor</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# gcd.sh: greatest common divisor   3&nbsp;#         Uses Euclid's algorithm   4&nbsp;   5&nbsp;#  The "greatest common divisor" (gcd) of two integers   6&nbsp;#+ is the largest integer that will divide both, leaving no remainder.   7&nbsp;   8&nbsp;#  Euclid's algorithm uses successive division.   9&nbsp;#  In each pass,  10&nbsp;#+ dividend &#60;---  divisor  11&nbsp;#+ divisor  &#60;---  remainder  12&nbsp;#+ until remainder = 0.  13&nbsp;#+ The gcd = dividend, on the final pass.  14&nbsp;#  15&nbsp;#  For an excellent discussion of Euclid's algorithm, see  16&nbsp;#  Jim Loy's site, http://www.jimloy.com/number/euclids.htm.  17&nbsp;  18&nbsp;  19&nbsp;# ------------------------------------------------------  20&nbsp;# Argument check  21&nbsp;ARGS=2  22&nbsp;E_BADARGS=65  23&nbsp;  24&nbsp;if [ $# -ne "$ARGS" ]  25&nbsp;then  26&nbsp;  echo "Usage: `basename $0` first-number second-number"  27&nbsp;  exit $E_BADARGS  28&nbsp;fi  29&nbsp;# ------------------------------------------------------  30&nbsp;  31&nbsp;  32&nbsp;gcd ()  33&nbsp;{  34&nbsp;  35&nbsp;  dividend=$1                    #  Arbitrary assignment.  36&nbsp;  divisor=$2                     #+ It doesn't matter which of the two is larger.  37&nbsp;                                 #  Why not?  38&nbsp;  39&nbsp;  remainder=1                    #  If uninitialized variable used in loop,  40&nbsp;                                 #+ it results in an error message  41&nbsp;                                 #+ on the first pass through loop.  42&nbsp;  43&nbsp;  until [ "$remainder" -eq 0 ]  44&nbsp;  do  45&nbsp;    let "remainder = $dividend % $divisor"  46&nbsp;    dividend=$divisor            # Now repeat with 2 smallest numbers.  47&nbsp;    divisor=$remainder  48&nbsp;  done                           # Euclid's algorithm  49&nbsp;  50&nbsp;}                                # Last $dividend is the gcd.  51&nbsp;  52&nbsp;  53&nbsp;gcd $1 $2  54&nbsp;  55&nbsp;echo; echo "GCD of $1 and $2 = $dividend"; echo  56&nbsp;  57&nbsp;  58&nbsp;# Exercise :  59&nbsp;# --------  60&nbsp;#  Check command-line arguments to make sure they are integers,  61&nbsp;#+ and exit the script with an appropriate error message if not.  62&nbsp;  63&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD><DT><SPANCLASS="TOKEN">+=</SPAN></DT><DD><P><SPANCLASS="QUOTE">"plus-equal"</SPAN> (increment variable by a constant)</P><P><TTCLASS="USERINPUT"><B>let "var += 5"</B></TT> results in	      <TTCLASS="VARNAME">var</TT> being incremented by	      <TTCLASS="LITERAL">5</TT>.</P></DD><DT><SPANCLASS="TOKEN">-=</SPAN></DT><DD><P><SPANCLASS="QUOTE">"minus-equal"</SPAN> (decrement variable by a constant)</P></DD><DT><SPANCLASS="TOKEN">*=</SPAN></DT><DD><P><SPANCLASS="QUOTE">"times-equal"</SPAN> (multiply variable by a constant)</P><P><TTCLASS="USERINPUT"><B>let "var *= 4"</B></TT> results in <TTCLASS="VARNAME">var</TT>	    being multiplied by <TTCLASS="LITERAL">4</TT>.</P></DD><DT><SPANCLASS="TOKEN">/=</SPAN></DT><DD><P><SPANCLASS="QUOTE">"slash-equal"</SPAN> (divide variable by a constant)</P></DD><DT><SPANCLASS="TOKEN">%=</SPAN></DT><DD><P><SPANCLASS="QUOTE">"mod-equal"</SPAN> (remainder of dividing variable by a constant)</P><P><ICLASS="EMPHASIS">Arithmetic operators often occur in an        <AHREF="moreadv.html#EXPRREF">expr</A> or <AHREF="internal.html#LETREF">let</A> expression.</I></P><DIVCLASS="EXAMPLE"><HR><ANAME="ARITHOPS"></A><P><B>Example 8-2. Using Arithmetic Operations</B></P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="90%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;#!/bin/bash   2&nbsp;# Counting to 11 in 10 different ways.   3&nbsp;   4&nbsp;n=1; echo -n "$n "   5&nbsp;   6&nbsp;let "n = $n + 1"   # let "n = n + 1"  also works.   7&nbsp;echo -n "$n "   8&nbsp;   9&nbsp;  10&nbsp;: $((n = $n + 1))  11&nbsp;#  ":" necessary because otherwise Bash attempts  12&nbsp;#+ to interpret "$((n = $n + 1))" as a command.  13&nbsp;echo -n "$n "  14&nbsp;  15&nbsp;(( n = n + 1 ))  16&nbsp;#  A simpler alternative to the method above.  17&nbsp;#  Thanks, David Lombard, for pointing this out.  18&nbsp;echo -n "$n "  19&nbsp;  20&nbsp;n=$(($n + 1))  21&nbsp;echo -n "$n "  22&nbsp;  23&nbsp;: $[ n = $n + 1 ]  24&nbsp;#  ":" necessary because otherwise Bash attempts  25&nbsp;#+ to interpret "$[ n = $n + 1 ]" as a command.  26&nbsp;#  Works even if "n" was initialized as a string.  27&nbsp;echo -n "$n "  28&nbsp;  29&nbsp;n=$[ $n + 1 ]  30&nbsp;#  Works even if "n" was initialized as a string.  31&nbsp;#* Avoid this type of construct, since it is obsolete and nonportable.  32&nbsp;#  Thanks, Stephane Chazelas.  33&nbsp;echo -n "$n "  34&nbsp;  35&nbsp;# Now for C-style increment operators.  36&nbsp;# Thanks, Frank Wang, for pointing this out.  37&nbsp;  38&nbsp;let "n++"          # let "++n"  also works.  39&nbsp;echo -n "$n "  40&nbsp;  41&nbsp;(( n++ ))          # (( ++n )  also works.  42&nbsp;echo -n "$n "  43&nbsp;  44&nbsp;: $(( n++ ))       # : $(( ++n )) also works.  45&nbsp;echo -n "$n "  46&nbsp;  47&nbsp;: $[ n++ ]         # : $[ ++n ]] also works  48&nbsp;echo -n "$n "  49&nbsp;  50&nbsp;echo  51&nbsp;  52&nbsp;exit 0</PRE></TD></TR></TABLE><HR></DIV></DD></DL></DIV><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>Integer variables in Bash are actually signed	<ICLASS="EMPHASIS">long</I> (32-bit) integers, in the range of	-2147483648 to 2147483647. An operation that takes a variable	outside these limits will give an erroneous result.

⌨️ 快捷键说明

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