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

📄 _chapter 12.htm

📁 linux、unix初学者的必读书籍 详细讲述了shell编程方法与技巧
💻 HTM
📖 第 1 页 / 共 3 页
字号:
declared as an integer, the built-in <span class="docEmphasis">let</span>
command allows arithmetic operations. See &quot;The let Command&quot; on page 732.) If you
attempt to assign a floating point number, <span class="docEmphasis">bash</span>
reports a syntax error<span class="docEmphasis">.</span> Numbers can also be
represented in different bases such as <span class="docEmphasis">binary, octal,</span>
and <span class="docEmphasis">hex.</span></p>
<h5 id="ch12list06" class="docExampleTitle">Example 12.6 </h5>
<pre>1   $ <span class="docEmphStrong">declare 杋 num</span>

2   $ <span class="docEmphStrong">num=hello</span>
    $ echo $num
    <span class="docEmphasis">0</span>

3   $ <span class="docEmphStrong">num=5 + 5</span>
    <span class="docEmphasis">bash: +: command not found</span>

4   $ <span class="docEmphStrong">num=5+5</span>
    $ echo $num
    <span class="docEmphasis">10</span>

5   $ <span class="docEmphStrong">num=4*6</span>
    $ echo $num
    <span class="docEmphasis">24</span>

6   $ <span class="docEmphStrong">num=&quot;4 * 6&quot;</span>
    $ echo $num
    <span class="docEmphasis">24</span>

7   $ <span class="docEmphStrong">num=6.5</span>
    <span class="docEmphasis">bash:</span> <span class="docEmphasis">num: 6.5: sytax error in expression (remainder of</span>
     <span class="docEmphasis">expression is &quot;.5&quot;)</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">declare</span> command
      with the <span class="docEmphasis">杋</span> option creates an integer
      variable <span class="docEmphasis">num.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Trying to assign the string <span class="docEmphasis">
      hello</span> to the integer variable <span class="docEmphasis">num</span>
      causes the string to be stored as zero.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The whitespace must be quoted or removed unless the
      <span class="docEmphasis">let</span> command is used.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The whitespace is removed and arithmetic is performed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Multiplication is performed and the result assigned to
      <span class="docEmphasis">num.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The whitespace is quoted so that the multiplication can
      be performed and to keep the shell from expanding the wildcard (*).</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">Because the variable is set to integer, adding a
      fractional part causes a <span class="docEmphasis">bash</span> syntax
      error.</span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText"><b>Listing Integers.</b> The <span class="docEmphasis">
declare</span> command with only the <span class="docEmphasis">杋</span>
argument will list all preset integers and their values, as shown in the
following display.</p>
<pre>$ <span class="docEmphStrong">declare 杋</span>
<span class="docEmphasis">declare -ir EUID=&quot;15&quot;          # effective user id</span>
<span class="docEmphasis">declare -ir PPID=&quot;235&quot;         # parent process id</span>
<span class="docEmphasis">declare -ir UID=&quot;15&quot;           # user id</span>
</pre>
<p class="docText"><b>Representing and Using Different Bases.</b> Numbers can be
represented in decimal (base 10, the default), octal (base 8), hexadecimal (base
16), and a range from base 2 to 36.</p>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">FORMAT</h2>
    <pre>variable=base#number-in-that-base
</pre>
    </td>
  </tr>
</table>

<h5 id="ch12list07" class="docExampleTitle">Example 12.7 </h5>
<pre><span class="docEmphStrong">n=2#101</span>   <span class="docEmphasis">Base is 2; number 101 is in base 2</span>
</pre>
<h5 id="ch12list08" class="docExampleTitle">Example 12.8 </h5>
<pre>(The Command Line)
1  $ <span class="docEmphStrong">declare -i x=017</span>
   $ echo $x
   <span class="docEmphasis">15</span>
2  $ <span class="docEmphStrong">x=2#101</span>
   $ echo $x
   <span class="docEmphasis">5</span>
3  $ <span class="docEmphStrong">x=8#17</span>
   $ echo $x
   <span class="docEmphasis">15</span>
4  $ <span class="docEmphStrong">x=16#b</span>
   $ echo $x
   <span class="docEmphasis">11</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">declare</span> function
      is used to assign an integer variable <span class="docEmphasis">x</span>
      the octal value <span class="docEmphasis">017.</span> Octal numbers must
      start with a leading zero. <span class="docEmphasis">15,</span> the
      decimal value of <span class="docEmphasis">017,</span> is printed.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable, <span class="docEmphasis">x,</span> is
      assigned the value of <span class="docEmphasis">101</span> (binary).
      <span class="docEmphasis">2</span> represents the base, separated by a #,
      and the number in that base, <span class="docEmphasis">101.</span> The
      value of <span class="docEmphasis">x</span> is printed as decimal,
      <span class="docEmphasis">5.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable <span class="docEmphasis">x</span> is
      assigned the value of <span class="docEmphasis">17</span> (octal). The
      value of <span class="docEmphasis">x</span> is printed as decimal,
      <span class="docEmphasis">15.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable <span class="docEmphasis">x</span> is
      assigned the value of <span class="docEmphasis">b</span> (hexadecimal).
      The value of <span class="docEmphasis">x</span> is decimal
      <span class="docEmphasis">11.</span></span></li>
    </ol>
    </span></td>
  </tr>
</table>

<p class="docText">The <span class="docEmphasis">let</span> Command. The
<span class="docEmphasis">let</span> command is a <span class="docEmphasis">bash</span>
shell built-in command that is used to perform integer arithmetic and numeric
expression testing. To see what <span class="docEmphasis">let</span> operators
your version of <span class="docEmphasis">bash</span> supports, type at the
prompt:</p>
<pre>help let
</pre>
<p class="docText">A list of the operators is also found in
<a class="docLink" href="#ch12table04">Table
12.4</a>.</p>
<h5 id="ch12list09" class="docExampleTitle">Example 12.9 </h5>
<pre>1   $ <span class="docEmphStrong">i=5</span>  <span class="docEmphasis">or</span> <span class="docEmphStrong">let i=5</span>

2   $ <span class="docEmphStrong">let i=i+1</span>
    $ <span class="docEmphStrong">echo $i</span>
    <span class="docEmphasis">6</span>

3   $ <span class="docEmphStrong">let &quot;i = i + 2&quot;</span>
    $ <span class="docEmphStrong">echo $i</span>
    <span class="docEmphasis">8</span>

4   $ <span class="docEmphStrong">let &quot;i+=1&quot;</span>
    $ <span class="docEmphStrong">echo $i</span>
    <span class="docEmphasis">9</span>

5   $ <span class="docEmphStrong">i=3</span>

6   $ <span class="docEmphStrong">(( i+=4))</span>
    $ <span class="docEmphStrong">echo $i</span>
    <span class="docEmphasis">7</span>

7   $ <span class="docEmphStrong">(( i=i-2 ))</span>
    $ <span class="docEmphStrong">echo $i</span>
    <span class="docEmphasis">5</span>
</pre>

<table cellSpacing="0" width="90%" border="1" align="center">
  <tr>
    <td>
    <h2 class="docSidebarTitle">EXPLANATION</h2>
    <span style="FONT-WEIGHT: bold">
    <ol class="docList" type="1">
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable <span class="docEmphasis">i</span> is
      assigned the value <span class="docEmphasis">5.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The <span class="docEmphasis">let</span> command will
      add <span class="docEmphasis">1</span> to the value of
      <span class="docEmphasis">i.</span> The <span class="docEmphasis">$</span>
      (dollar sign) is not required for variable substitution when performing
      arithmetic.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The quotes are needed if the arguments contain
      whitespace.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The shortcut operator, <span class="docEmphasis">+=,</span>
      is used to add <span class="docEmphasis">1</span> to the value of
      <span class="docEmphasis">i.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The variable <span class="docEmphasis">i</span> is
      assigned the value 5.</span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">The double parentheses can be used to replace
      <span class="docEmphasis">let.</span><sup class="docFootnote"><a class="docLink" href="#ch12fn05">[a]</a></sup></p>
      <p class="docList">4 is added and assigned to <span class="docEmphasis">i.</span></span></li>
      <li><span style="FONT-WEIGHT: normal">
      <p class="docList">2 is subtracted from <span class="docEmphasis">i.</span>
      We could have also written <span class="docEmphasis">i

⌨️ 快捷键说明

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