﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-cygw-文章分类-Linux/Unix</title><link>http://www.cnitblog.com/cygw/category/7706.html</link><description /><language>zh-cn</language><lastBuildDate>Wed, 28 Sep 2011 19:26:46 GMT</lastBuildDate><pubDate>Wed, 28 Sep 2011 19:26:46 GMT</pubDate><ttl>60</ttl><item><title>Bash的基本语法</title><link>http://www.cnitblog.com/cygw/articles/50288.html</link><dc:creator>cygw</dc:creator><author>cygw</author><pubDate>Wed, 15 Oct 2008 13:43:00 GMT</pubDate><guid>http://www.cnitblog.com/cygw/articles/50288.html</guid><wfw:comment>http://www.cnitblog.com/cygw/comments/50288.html</wfw:comment><comments>http://www.cnitblog.com/cygw/articles/50288.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/cygw/comments/commentRss/50288.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/cygw/services/trackbacks/50288.html</trackback:ping><description><![CDATA[<span class=Apple-style-span style="WORD-SPACING: 0px; FONT: 16px simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0">
<div style="BACKGROUND-COLOR: #000000"><a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/bash-intro.html"></a><span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/bash-spec.html"></a><span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/index.html"></a>
<h1 style="COLOR: rgb(255,255,0)" align=left>2&nbsp;&nbsp;&nbsp;&nbsp;BASH 的基本语法</h1>
<ul>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">最简单的例子 —— Hello World!</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">关于输入、输出和错误输出</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">BASH 中对变量的规定（与 C 语言的异同）</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">BASH 中的基本流程控制语法</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">函数的使用</p>
    </li>
</ul>
<h3 style="COLOR: rgb(255,0,255)">2.1&nbsp;&nbsp;&nbsp;&nbsp;最简单的例子 —— Hello World!</h3>
<p style="COLOR: rgb(0,255,255)">几乎所有的讲解编程的书给读者的第一个例子都是 Hello World 程序，那么我们今天也就从这个例子出发，来逐步了解 BASH。</p>
<p style="COLOR: rgb(0,255,255)">用 vi 编辑器编辑一个<span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/hello"><u>hello 文件</u></a>如下：<br><br><font color=#ffffff><strong>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br># This is a very simple example<br>echo Hello World</strong></font></p>
<p style="COLOR: rgb(0,255,255)">这样最简单的一个 BASH 程序就编写完了。这里有几个问题需要说明一下：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff>一，第一行的 #! 是什么意思<br>二，第一行的 /bin/bash 又是什么意思<br>三，第二行是注释吗<span class=Apple-converted-space>&nbsp;</span><br>四，echo 语句<br>五，如何执行该程序</font></p>
<p style="COLOR: rgb(0,255,255)">#! 是说明 hello 这个文件的类型的，有点类似于 Windows 系统下用不同文件后缀来表示不同文件类型的意思（但不相同）。Linux 系统根据 "#!" 及该字串后面的信息确定该文件的类型，关于这一问题同学们回去以后可以通过 "man magic"命令 及 /usr/share/magic 文件来了解这方面的更多内容。在 BASH 中 第一行的 "#!" 及后面的 "/bin/bash" 就表明该文件是一个 BASH 程序，需要由 /bin 目录下的 bash 程序来解释执行。BASH 这个程序一般是存放在 /bin 目录下，如果你的 Linux 系统比较特别，bash 也有可能被存放在 /sbin 、/usr/local/bin 、/usr/bin 、/usr/sbin 或 /usr/local/sbin 这样的目录下；如果还找不到，你可以用 "locate bash" "find / -name bash 2&gt; /dev/null" 或 "whereis bash" 这三个命令找出 bash 所在的位置；如果仍然找不到，那你可能需要自己动手安装一个 BASH 软件包了。</p>
<p style="COLOR: rgb(0,255,255)">第二行的 "# This is a ..." 就是 BASH 程序的注释，在 BASH 程序中从&#8220;#&#8221;号（注意：后面紧接着是&#8220;!&#8221;号的除外）开始到行尾的多有部分均被看作是程序的注释。的三行的 echo 语句的功能是把 echo 后面的字符串输出到标准输出中去。由于 echo 后跟的是 "Hello World" 这个字符串，因此 "Hello World"这个字串就被显示在控制台终端的屏幕上了。需要注意的是 BASH 中的绝大多数语句结尾处都没有分号。</p>
<p style="COLOR: rgb(0,255,255)">如何执行该程序呢？有两种方法：一种是显式制定 BASH 去执行：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong>$ bash hello 或<br>$ sh hello （这里 sh 是指向 bash 的一个链接，&#8220;lrwxrwxrwx 1 root root 4 Aug 20 05:41 /bin/sh -&gt; bash&#8221;）</strong></font></p>
<p style="COLOR: rgb(0,255,255)">或者可以先将 hello 文件改为可以执行的文件，然后直接运行它，此时由于 hello 文件第一行的 "#! /bin/bash" 的作用，系统会自动用/bin/bash 程序去解释执行 hello 文件的：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong>$ chmod u+x hello<br>$ ./hello</strong></font></p>
<p style="COLOR: rgb(0,255,255)">此处没有直接 &#8220;$ hello&#8221;是因为当前目录不是当前用户可执行文件的默认目录，而将当前目录&#8220;.&#8221;设为默认目录是一个不安全的设置。</p>
<p style="COLOR: rgb(0,255,255)">需要注意的是，BASH 程序被执行后，实际上 Linux 系统是另外开设了一个进程来运行的。</p>
<br>
<h3 style="COLOR: rgb(255,0,255)">2.2&nbsp;&nbsp;&nbsp;&nbsp;关于输入、输出和错误输出</h3>
<p style="COLOR: rgb(0,255,255)"><em></em>在字符终端环境中，标准输入/标准输出的概念很好理解。输入即指对一个应用程序或命令的输入，无论是从键盘输入还是从别的文件输入；输出即指应用程序或命令产生的一些信息；与 Windows 系统下不同的是，Linux 系统下还有一个标准错误输出的概念，这个概念主要是为程序调试和系统维护目的而设置的，错误输出于标准输出分开可以让一些高级的错误信息不干扰正常的输出信息，从而方便一般用户的使用。</p>
<p style="COLOR: rgb(0,255,255)">在 Linux 系统中：标准输入(stdin)默认为键盘输入；标准输出(stdout)默认为屏幕输出；标准错误输出(stderr)默认也是输出到屏幕（上面的 std 表示 standard）。在 BASH 中使用这些概念时一般将标准输出表示为 1，将标准错误输出表示为 2。下面我们举例来说明如何使用他们，特别是标准输出和标准错误输出。</p>
<p style="COLOR: rgb(0,255,255)">输入、输出及标准错误输出主要用于 I/O 的重定向，就是说需要改变他们的默认设置。先看这个例子：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong>$ ls &gt; ls_result<br>$ ls -l &gt;&gt; ls_result</strong></font></p>
<p style="COLOR: rgb(0,255,255)">上面这两个命令分别将 ls 命令的结果输出重定向到 ls_result 文件中和追加到 ls_result 文件中，而不是输出到屏幕上。"&gt;"就是输出（标准输出和标准错误输出）重定向的代表符号，连续两个 "&gt;" 符号，即 "&gt;&gt;" 则表示不清除原来的而追加输出。下面再来看一个稍微复杂的例子：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ find /home -name lost* 2&gt; err_result</font></strong></p>
<p style="COLOR: rgb(0,255,255)">这个命令在 "&gt;" 符号之前多了一个 "2"，"2&gt;" 表示将标准错误输出重定向。由于 /home 目录下有些目录由于权限限制不能访问，因此会产生一些标准错误输出被存放在 err_result 文件中。大家可以设想一下<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff><em>find /home -name lost* 2&gt;&gt;err_result</em></font><span class=Apple-converted-space>&nbsp;</span>命令会产生什么结果？</p>
<p style="COLOR: rgb(0,255,255)">如果直接执行<span class=Apple-converted-space>&nbsp;</span><em><font color=#ffffff>find /home -name lost* &gt; all_result</font></em><span class=Apple-converted-space>&nbsp;</span>，其结果是只有标准输出被存入 all_result 文件中，要想让标准错误输出和标准输入一样都被存入到文件中，那该怎么办呢？看下面这个例子：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ find /home -name lost* &gt; all_result 2&gt;&amp; 1</font></strong></p>
<p style="COLOR: rgb(0,255,255)">上面这个例子中将首先将标准错误输出也重定向到标准输出中，再将标准输出重定向到 all_result 这个文件中。这样我们就可以将所有的输出都存储到文件中了。为实现上述功能，还有一种简便的写法如下：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ find /home -name lost* &gt;&amp; all_result</font></strong></p>
<p style="COLOR: rgb(0,255,255)">如果那些出错信息并不重要，下面这个命令可以让你避开众多无用出错信息的干扰：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ find /home -name lost* 2&gt; /dev/null</font></strong></p>
<p style="COLOR: rgb(0,255,255)"></p>
<p style="COLOR: rgb(0,255,255)">同学们回去后还可以再试验一下如下几种重定向方式，看看会出什么结果，为什么？</p>
<p style="COLOR: rgb(0,255,255)"></p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ find /home -name lost* &gt; all_result 1&gt;&amp; 2<span class=Apple-converted-space>&nbsp;</span><br>$ find /home -name lost* 2&gt; all_result 1&gt;&amp; 2<br>$ find /home -name lost* 2&gt;&amp; 1 &gt; all_result</font></strong></p>
<p style="COLOR: rgb(0,255,255)">另外一个非常有用的重定向操作符是 "-"，请看下面这个例子：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>$ (cd /source/directory &amp;&amp; tar cf - . ) | (cd /dest/directory &amp;&amp; tar xvfp -)</font></strong></p>
<p style="COLOR: rgb(0,255,255)">该命令表示把 /source/directory 目录下的所有文件通过压缩和解压，快速的全部移动到 /dest/directory 目录下去，这个命令在 /source/directory 和 /dest/directory 不处在同一个文件系统下时将显示出特别的优势。</p>
<p style="COLOR: rgb(0,255,255)">下面还几种不常见的用法：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>n&lt;&amp;- 表示将 n 号输入关闭<span class=Apple-converted-space>&nbsp;</span><br>&lt;&amp;- 表示关闭标准输入（键盘）<br>n&gt;&amp;- 表示将 n 号输出关闭<br>&gt;&amp;- 表示将标准输出关闭</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">&nbsp;</p>
<h3 style="COLOR: rgb(255,0,255)">2.3&nbsp;&nbsp;&nbsp;&nbsp;BASH 中对变量的规定（与 C 语言的异同）</h3>
<p style="COLOR: rgb(0,255,255)">好了下面我们进入正题，先看看 BASH 中的变量是如何定义和使用的。对于熟悉 C 语言的程序员，我们将解释 BASH 中的定义和用法与 C 语言中有何不同。</p>
<h4 style="COLOR: rgb(255,255,0)">2.3.1. BASH 中的变量介绍</h4>
<p style="COLOR: rgb(0,255,255)">我们先来从整体上把握一下 BASH 中变量的用法，然后再去分析 BASH 中变量使用与 C 语言中的不同。BASH 中的变量都是不能含有保留字，不能含有 "-" 等保留字符，也不能含有空格。</p>
<h4 style="COLOR: rgb(255,255,0)">2.3.1.1 简单变量</h4>
<p style="COLOR: rgb(0,255,255)">在 BASH 中变量定义是不需要的，没有 "int i" 这样的定义过程。如果想用一个变量，只要他没有在前面被定义过，就直接可以用，当然你使用该变量的第一条语句应该是对他赋初值了，如果你不赋初值也没关系，只不过该变量是空（ 注意：是 NULL，不是 0 ）。不给变量赋初值虽然语法上不反对，但不是一个好的编程习惯。好了我们看看下面的例子：</p>
<p style="COLOR: rgb(0,255,255)">首先用 vi 编辑下面这个<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/hello2"><u>文件 hello2</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br># give the initialize value to STR<br>STR="Hello World"<span class=Apple-converted-space>&nbsp;</span><br>echo $STR<span class=Apple-converted-space>&nbsp;</span><br></strong></font><br>在上面这个程序中我们需要注意下面几点：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff>一，变量赋值时，'='左右两边都不能有空格；<br>二，BASH 中的语句结尾不需要分号（";"）；<br>三，除了在变量赋值和在FOR循环语句头中，BASH 中的变量使用必须在变量前加"$"符号，同学们可以将上面程序中第三行改为 "echo STR" 再试试，看看会出什么结果。<br>四，由于 BASH 程序是在一个新的进程中运行的，所以该程序中的变量定义和赋值不会改变其他进程或原始 Shell 中同名变量的值，也不会影响他们的运行。</font></p>
<p style="COLOR: rgb(0,255,255)">更细致的文档甚至提到以但引号括起来的变量将不被 BASH 解释为变量，如 '$STR' ，而被看成为纯粹的字符串。而且更为标准的变量引用方式是 ${STR} 这样的，$STR 自不过是对 ${STR} 的一种简化。在复杂情况下（即有可能产生歧义的地方）最好用带 {} 的表示方式。</p>
<p style="COLOR: rgb(0,255,255)">BASH 中的变量既然不需要定义，也就没有类型一说，一个变量即可以被定义为一个字符串，也可以被再定义为整数。如果对该变量进行整数运算，他就被解释为整数；如果对他进行字符串操作，他就被看作为一个字符串。请看下面的<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/x"><u>例子</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br>x=1999<span class=Apple-converted-space>&nbsp;</span><br>let "x = $x + 1"<span class=Apple-converted-space>&nbsp;</span><br>echo $x<span class=Apple-converted-space>&nbsp;</span><br>x="olympic'"$x<span class=Apple-converted-space>&nbsp;</span><br>echo $x</font></strong></p>
<p style="COLOR: rgb(0,255,255)">关于整数变量计算，有如下几种："<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff>+ - * / %</font><span class=Apple-converted-space>&nbsp;</span>"，他们的意思和字面意思相同。整数运算一般通过 let 和 expr 这两个指令来实现，如对变量 x 加 1 可以写作：<font color=#ffffff>let "x = $x + 1"</font><span class=Apple-converted-space>&nbsp;</span>或者<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff>x=`expr $x + 1`</font></p>
<p style="COLOR: rgb(0,255,255)">在比较操作上，整数变量和字符串变量各不相同，详见下表：</p>
<div align=center>
<table width="75%" border=1>
    <tbody>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>对应的操作</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>整数操作</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>字符串操作</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>相同</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-eq</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>=</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>不同</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-ne</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>!=</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>大于</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-gt</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>&gt;</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>小于</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-lt</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>&lt;</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>大于或等于</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-ge</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center></div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>小于或等于</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-le</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center></div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>为空</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center></div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-z</div>
            </td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="26%">
            <div style="COLOR: #ffcc00" align=center>不为空</div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center></div>
            </td>
            <td style="COLOR: #ffcc00" width="37%">
            <div style="COLOR: #ffcc00" align=center>-n</div>
            </td>
        </tr>
    </tbody>
</table>
</div>
<p style="COLOR: rgb(0,255,255)"><br>比如：</p>
<p style="COLOR: rgb(0,255,255)">比较字符串 a 和 b 是否相等就写作：<font color=#ffffff>if [ $a = $b ]</font><br>判断字符串 a 是否为空就写作：<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff>if [ -z $a ]</font><br>判断整数变量 a 是否大于 b 就写作：<font color=#ffffff>if [ $a -gt $b ]</font></p>
<p style="COLOR: rgb(0,255,255)">更细致的文档推荐在字符串比较时尽量不要使用 -n ，而用 ! -z 来代替。（其中符号 "!" 表示求反操作）</p>
<p style="COLOR: rgb(0,255,255)">BASH 中的变量除了用于对 整数 和 字符串 进行操作以外，另一个作用是作为文件变量。BASH 是 Linux 操作系统的 Shell，因此系统的文件必然是 BASH 需要操作的重要对象，如<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff>if [ -x /root ]</font><span class=Apple-converted-space>&nbsp;</span>可以用于判断 /root 目录是否可以被当前用户进入。下表列出了 BASH 中用于判断文件属性的操作符：</p>
<div align=center>
<table width="50%" border=1>
    <tbody>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>运算符</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">含义（ 满足下面要求时返回 TRUE ）</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-e file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 已经存在</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-f file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 是普通文件</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-s file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 大小不为零</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-d file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 是一个目录</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-r file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 对当前用户可以读取</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-w file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 对当前用户可以写入</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-x file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 对当前用户可以执行</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-g file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 的 GID 标志被设置</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-u file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 的 UID 标志被设置</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-O file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 是属于当前用户的</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>-G file</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file 的组 ID 和当前用户相同</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>file1 -nt file2</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file1 比 file2 更新</td>
        </tr>
        <tr>
            <td style="COLOR: #ffcc00" width="27%">
            <div style="COLOR: #ffcc00" align=center>file1 -ot file2</div>
            </td>
            <td style="COLOR: #ffcc00" width="73%">文件 file1 比 file2 更老</td>
        </tr>
    </tbody>
</table>
</div>
<p style="COLOR: rgb(0,255,255)"><br>注意：上表中的 file 及 file1、file2 都是指某个文件或目录的路径。</p>
<h4 style="COLOR: rgb(255,255,0)">2.3.1.1. 关于局部变量</h4>
<p style="COLOR: rgb(0,255,255)">在 BASH 程序中如果一个变量被使用了，那么直到该程序的结尾，该变量都一直有效。为了使得某个变量存在于一个局部程序块中，就引入了局部变量的概念。BASH 中，在变量首次被赋初值时加上 local 关键字就可以声明一个局部变量，如下面这个<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/local"><u>例子</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br>HELLO=Hello<span class=Apple-converted-space>&nbsp;</span><br>function hello {<span class=Apple-converted-space>&nbsp;</span><br>　　local HELLO=World<span class=Apple-converted-space>&nbsp;</span><br>　　echo $HELLO<span class=Apple-converted-space>&nbsp;</span><br>}<span class=Apple-converted-space>&nbsp;</span><br>echo $HELLO<span class=Apple-converted-space>&nbsp;</span><br>hello<span class=Apple-converted-space>&nbsp;</span><br>echo $HELLO</font></strong></p>
<p style="COLOR: rgb(0,255,255)">该程序的执行结果是：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>Hello<br>World<br>Hello</font></strong></p>
<p style="COLOR: rgb(0,255,255)">这个执行结果表明全局变量 $HELLO 的值在执行函数 hello 时并没有被改变。也就是说局部变量 $HELLO 的影响只存在于函数那个程序块中。</p>
<h4 style="COLOR: rgb(255,255,0)">2.3.2. BASH 中的变量与 C 语言中变量的区别</h4>
<p style="COLOR: rgb(0,255,255)">这里我们为原来不熟悉 BASH 编程，但是非常熟悉 C 语言的程序员总结一下在 BASH 环境中使用变量需要注意的问题。</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff>1，BASH 中的变量在引用时都需要在变量前加上 "$" 符号（ 第一次赋值及在For循环的头部不用加 "$"符号 ）；<br>2，BASH 中没有浮点运算，因此也就没有浮点类型的变量可用；<br>3，BASH 中的整形变量的比较符号与 C 语言中完全不同，而且整形变量的算术运算也需要经过 let 或 expr 语句来处理；</font></p>
<h3 style="COLOR: rgb(255,0,255)">2.4&nbsp;&nbsp;&nbsp;&nbsp;BASH 中的基本流程控制语法</h3>
<p style="COLOR: rgb(0,255,255)">BASH 中几乎含有 C 语言中常用的所有控制结构，如条件分支、循环等，下面逐一介绍。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.1 if...then...else</h4>
<p style="COLOR: rgb(0,255,255)">if 语句用于判断和分支，其语法规则和 C 语言的 if 非常相似。其几种基本结构为：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>if [ expression ]<br>then<br>　　statments<br>fi</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">或者</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong><em>if [ expression ]<br>then<br>　　statments<br>else<br>　　statments<br>fi</em></strong></font></p>
<p style="COLOR: rgb(0,255,255)">或者</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong><em>if [ expression ]<br>then<br>　　statments<br>else if [ expression ]<br>　　then<span class=Apple-converted-space>&nbsp;</span><br>　　　　statments<br>　　else<br>　　　　statments<span class=Apple-converted-space>&nbsp;</span><br>fi</em></strong></font></p>
<p style="COLOR: rgb(0,255,255)">或者</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong><em>if [ expression ]<br>then<br>　　statments<br>elif [ expression ]<br>　　then<br>　　　　statments<br>　　else<br>　　　　statments<br>fi</em></strong></font></p>
<p style="COLOR: rgb(0,255,255)">值得说明的是如果你将 if 和 then 简洁的写在一行里面，就必须在 then 前面加上分号，如：<font color=#ffffff>if [ expression ]; then ...<span class=Apple-converted-space>&nbsp;</span></font>。下面这个<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/if"><u>例子</u></a>说明了如何使用 if 条件判断语句：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br><br>if [ $1 -gt 90 ]<span class=Apple-converted-space>&nbsp;</span><br>then<span class=Apple-converted-space>&nbsp;</span><br>　　echo "Good, $1"<span class=Apple-converted-space>&nbsp;</span><br>elif [ $1 -gt 70 ]<span class=Apple-converted-space>&nbsp;</span><br>　　then<span class=Apple-converted-space>&nbsp;</span><br>　　　　echo "OK, $1"<span class=Apple-converted-space>&nbsp;</span><br>　　else<span class=Apple-converted-space>&nbsp;</span><br>　　　　echo "Bad, $1"<span class=Apple-converted-space>&nbsp;</span><br>fi<span class=Apple-converted-space>&nbsp;</span><br><br>exit 0</font></strong></p>
<p style="COLOR: rgb(0,255,255)">上面例子中的 $1 是指命令行的第一个参数，这个会在后面的&#8220;<a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/bash-spec.html"><u>BASH 中的特殊保留字</u></a>&#8221;中讲解。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.2 for</h4>
<p style="COLOR: rgb(0,255,255)">for 循环结构与 C 语言中有所不同，在 BASH 中 for 循环的基本结构是：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>for $var in [list]<br>do<br>　　statments<br>done</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">其中 $var 是循环控制变量，[list] 是 $var 需要遍历的一个集合，do/done 对包含了循环体，相当于 C 语言中的一对大括号。另外如果do 和 for 被写在同一行，必须在 do 前面加上 ";"。如：<span class=Apple-converted-space>&nbsp;</span><font color=#ffffff>for $var in [list]; do</font><span class=Apple-converted-space>&nbsp;</span>。下面是一个运用 for 进行循环的例子：</p>
<p style="COLOR: rgb(0,255,255)"><font color=#ffffff><strong>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br><br>for day in Sun Mon Tue Wed Thu Fri Sat<span class=Apple-converted-space>&nbsp;</span><br>do<span class=Apple-converted-space>&nbsp;</span><br>　　echo $day<span class=Apple-converted-space>&nbsp;</span><br>done<span class=Apple-converted-space>&nbsp;</span><br><br># 如果列表被包含在一对双引号中，则被认为是一个元素<span class=Apple-converted-space>&nbsp;</span><br>for day in "Sun Mon Tue Wed Thu Fri Sat"<span class=Apple-converted-space>&nbsp;</span><br>do<span class=Apple-converted-space>&nbsp;</span><br>　　echo $day<span class=Apple-converted-space>&nbsp;</span><br>done<span class=Apple-converted-space>&nbsp;</span><br><br>exit 0</strong></font></p>
<p style="COLOR: rgb(0,255,255)">注意上面的例子中，在 for 所在那行的变量 day 是没有加 "$" 符号的，而在循环体内，echo 所在行变量 $day 是必须加上 "$" 符号的。另外如果写成 for day 而没有后面的 in [list] 部分，则 day 将取遍命令行的所有参数。如<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/for2"><u>这个程序</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br><br>for param<span class=Apple-converted-space>&nbsp;</span><br>do<span class=Apple-converted-space>&nbsp;</span><br>　　echo $param<span class=Apple-converted-space>&nbsp;</span><br>done<span class=Apple-converted-space>&nbsp;</span><br><br>exit 0</font></strong></p>
<p style="COLOR: rgb(0,255,255)">上面这个程序将列出所有命令行参数。for 循环结构的循环体被包含在 do/done 对中，这也是后面的 while、until 循环所具有的特点。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.3 while</h4>
<p style="COLOR: rgb(0,255,255)">while 循环的基本结构是：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>while [ condition ]<br>do<br>　　statments<br>done</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">这个结构请大家自己编写一个例子来验证。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.4 until</h4>
<p style="COLOR: rgb(0,255,255)">until 循环的基本结构是：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>until [ condition is TRUE ]<br>do<br>　　statments<br>done</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">这个结构也请大家自己编写一个例子来验证。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.5 case</h4>
<p style="COLOR: rgb(0,255,255)">BASH 中的 case 结构与 C 语言中的 switch 语句的功能比较类似，可以用于进行多项分支控制。其基本结构是：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>case "$var" in<br>　condition1 )<br>　　statments1;;<br>　condition2 )<br>　　statments2;;<br>　...<br>　* )<br>　　default statments;;<br>esac</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">下面这个程序是运用 case 结构进行分支执行的<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/case"><u>例子</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br><br>echo "Hit a key, then hit return."<span class=Apple-converted-space>&nbsp;</span><br>read Keypress<span class=Apple-converted-space>&nbsp;</span><br><br>case "$Keypress" in<span class=Apple-converted-space>&nbsp;</span><br>　[a-z] ) echo "Lowercase letter";;<span class=Apple-converted-space>&nbsp;</span><br>　[A-Z] ) echo "Uppercase letter";;<span class=Apple-converted-space>&nbsp;</span><br>　[0-9] ) echo "Digit";;<span class=Apple-converted-space>&nbsp;</span><br>　* ) echo "Punctuation, whitespace, or other";;<span class=Apple-converted-space>&nbsp;</span><br>esac<span class=Apple-converted-space>&nbsp;</span><br><br>exit 0</font></strong></p>
<p style="COLOR: rgb(0,255,255)">上面例子中的第四行 "read Keypress" 一句中的 read 语句表示从键盘上读取输入。这个命令将在本讲义的<span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/bash-adv.html"><u>BASH 的其他高级问题</u></a>中讲解。</p>
<h4 style="COLOR: rgb(255,255,0)">2.4.6 break/continue</h4>
<p style="COLOR: rgb(0,255,255)">熟悉 C 语言编程的都很熟悉 break 语句和 continue 语句。BASH 中同样有这两条语句，而且作用和用法也和 C 语言中相同，break 语句可以让程序流程从当前循环体中完全跳出，而 continue 语句可以跳过当次循环的剩余部分并直接进入下一次循环。</p>
<p style="COLOR: rgb(0,255,255)">&nbsp;</p>
<h3 style="COLOR: rgb(255,0,255)">2.5&nbsp;&nbsp;&nbsp;&nbsp;函数的使用</h3>
<p style="COLOR: rgb(0,255,255)">BASH 是一个相对简单的脚本语言，不过为了方便结构化的设计，BASH 中也提供了函数定义的功能。BASH 中的函数定义很简单，只要向下面这样写就可以了：</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>function my_funcname {<span class=Apple-converted-space>&nbsp;</span><br>　code block<br>}</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">或者</p>
<p style="COLOR: rgb(0,255,255)"><strong><em><font color=#ffffff>my_funcname() {<span class=Apple-converted-space>&nbsp;</span><br>　code block<br>}</font></em></strong></p>
<p style="COLOR: rgb(0,255,255)">上面的第二种写法更接近于 C 语言中的写法。BASH 中要求函数的定义必须在函数使用之前，这是和 C 语言用头文件说明函数方法的不同。</p>
<p style="COLOR: rgb(0,255,255)">更进一步的问题是如何给函数传递参数和获得返回值。BASH 中函数参数的定义并不需要在函数定义处就制定，而只需要在函数被调用时用 BASH 的保留变量 $1 $2 ... 来引用就可以了；BASH 的返回值可以用 return 语句来指定返回一个特定的整数，如果没有 return 语句显式的返回一个返回值，则返回值就是该函数最后一条语句执行的结果（一般为 0，如果执行失败返回错误码）。函数的返回值在调用该函数的程序体中通过 $? 保留字来获得。下面我们就来看一个用函数来计算整数平方的<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/func"><u>例子</u></a>：</p>
<p style="COLOR: rgb(0,255,255)"><strong><font color=#ffffff>#!/bin/bash<span class=Apple-converted-space>&nbsp;</span><br><br>square() {<span class=Apple-converted-space>&nbsp;</span><br>　let "res = $1 * $1"<span class=Apple-converted-space>&nbsp;</span><br>　return $res<span class=Apple-converted-space>&nbsp;</span><br>}<span class=Apple-converted-space>&nbsp;</span><br><br>square $1<span class=Apple-converted-space>&nbsp;</span><br>result=$?<span class=Apple-converted-space>&nbsp;</span><br>echo $result<span class=Apple-converted-space>&nbsp;</span><br><br>exit 0</font></strong></p>
<p style="COLOR: rgb(0,255,255)">&nbsp;</p>
</span></div>
<img src ="http://www.cnitblog.com/cygw/aggbug/50288.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/cygw/" target="_blank">cygw</a> 2008-10-15 21:43 <a href="http://www.cnitblog.com/cygw/articles/50288.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Bash的历史和概貌</title><link>http://www.cnitblog.com/cygw/articles/50287.html</link><dc:creator>cygw</dc:creator><author>cygw</author><pubDate>Wed, 15 Oct 2008 13:36:00 GMT</pubDate><guid>http://www.cnitblog.com/cygw/articles/50287.html</guid><wfw:comment>http://www.cnitblog.com/cygw/comments/50287.html</wfw:comment><comments>http://www.cnitblog.com/cygw/articles/50287.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/cygw/comments/commentRss/50287.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/cygw/services/trackbacks/50287.html</trackback:ping><description><![CDATA[<div style="BORDER-RIGHT: #666666 1px solid; BORDER-TOP: #666666 1px solid; BORDER-LEFT: #666666 1px solid; BORDER-BOTTOM: #666666 1px solid; BACKGROUND-COLOR: #000000"><span class=Apple-style-span style="WORD-SPACING: 0px; FONT: 16px simsun; TEXT-TRANSFORM: none; COLOR: rgb(0,0,0); TEXT-INDENT: 0px; WHITE-SPACE: normal; LETTER-SPACING: normal; BORDER-COLLAPSE: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0">
<div style="COLOR: #000000; BACKGROUND-COLOR: #000000"><a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/bash-syntax.html"></a><span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/index.html"></a>
<h1 style="COLOR: rgb(255,255,0)" align=left>1&nbsp;&nbsp;&nbsp;&nbsp;BASH 的历史和概貌</h1>
<ul>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">BASH 是什么的缩写</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">Linux 中的其他 Shell</p>
    <li style="COLOR: rgb(255,255,255)">
    <p style="COLOR: rgb(0,255,255)">BASH 的主要特点</p>
    </li>
</ul>
<p style="COLOR: rgb(0,255,255)">为什么要学习 BASH 编程呢？因为 BASH 是每个 Linux 发行版都带有的一个标准基础软件，所以学会在 BASH 下编制一些小程序就可以让你对 Linux 系统的管理应付自如；其次 BASH 非常简单，如果不深究 BASH 语法中的细节的话，你可以用 1 个小时就学会他，应该说 BASH 比 HTML 要更容易学；最后即使你不打算用 BASH 编程，但是 Linux 系统中的许多配置文件和脚本都是 BASH 的语法，不懂一点 BASH 的知识就不能很好的理解和使用 Linux 。其实最简单的 BASH 就和 DOS 下的批处理文件类似，只要把要执行的命令一行一行写出来就行。</p>
<p style="COLOR: rgb(0,255,255)"><br></p>
<h3 style="COLOR: rgb(255,0,255)">1.1&nbsp;&nbsp;&nbsp;&nbsp;BASH 是什么的缩写</h3>
<p style="COLOR: rgb(0,255,255)">BASH = GNU Bourne-Again SHell，BASH 是 GNU 组织开发和推广的一个项目，不过也有一种风趣的说法认为 BASH = Born-Again Shell。</p>
<p style="COLOR: rgb(0,255,255)">BASH 是 Linux 操作系统上的一个 Shell，是由 /bin/bash 解释执行的。BASH 支持 IEEE POSIX P1003.2/ISO 9945.2 脚本语言工具标准。</p>
<br>
<h3 style="COLOR: rgb(255,0,255)">1.2&nbsp;&nbsp;&nbsp;&nbsp;Linux 系统中的其他 Shell</h3>
<p style="COLOR: rgb(0,255,255)">Shell 的概念是从 UNIX 系统中引用过来的，在 Linux 和 UNIX 系统里现在存在这多种不同的 Shell 可以使用。最常用的几种是</p>
<p style="COLOR: rgb(0,255,255)">Bourne shell (sh)<span class=Apple-converted-space>&nbsp;</span><br>C shell (csh)<span class=Apple-converted-space>&nbsp;</span><br>Korn shell (ksh)</p>
<p style="COLOR: rgb(0,255,255)">三种shell 都有它们的优点和缺点：</p>
<p style="COLOR: rgb(0,255,255)">Bourne shell 的作者是 Steven Bourne，它是 UNIX 最初使用的 Shell 并且在每种 UNIX 上都可以使用。Bourne Shell 在 Shell 编程方面相当优秀，但在处理与用户的交互方面作得不如其他几种 Shell；</p>
<p style="COLOR: rgb(0,255,255)">C Shell 由 Bill Joy 所写，它更多的考虑了用户界面的友好性，支持象命令补齐（command-line completion）等一些 Bourne Shell 所不支持的特性，普遍认为C shell 的编程接口做的不如 Bourne Shell，但 C Shell 还是被很多 C 程序员使用，因为 C Shell 的语法和 C 语言很相似，这也是 C Shell 名称的由来；</p>
<p style="COLOR: rgb(0,255,255)">Korn Shell (ksh) 由 Dave Korn 所写，它集合了 C Shell 和 Bourne Shell 的优点并且和 Bourne Shell 完全兼容。</p>
<p style="COLOR: rgb(0,255,255)">除了这些 Shell 以外，许多其他的 Shell 程序吸收了这些原来的 Shell 程序的优点而成为新的 Shell 。在 Linux 上常见的有 tcsh (csh 的扩展)，Bourne Again shell(bash, sh 的扩展), 和Public Domain Korn shell (pdksh, ksh 的扩展)。</p>
<p style="COLOR: rgb(0,255,255)">BASH 是大多数 Linux 系统的缺省 Shell。 BASH 只是 Linux 操作系统上的一种 Shell，虽然他是一个比较优秀和主流的 Shell，但是针对不同用户的不同需要和喜好，在 Linux 操作系统上还有几种其他的 Shell，如：ksh、csh、zsh 等。</p>
<p style="COLOR: rgb(0,255,255)"><em></em></p>
<h3 style="COLOR: rgb(255,0,255)">1.3&nbsp;&nbsp;&nbsp;&nbsp;BASH 的主要特点</h3>
<p style="COLOR: rgb(0,255,255)">Bourne Again shell (bash), 正如它的名字所暗示的，是 Bourne shell 的扩展。BASH 与 Bourne shell 完全向后兼容，并且在 Bourne shell 的基础上增加和增强了很多特性。BASH 也包含了很多 csh 和 Korn Shell 里的优点，使得 BASH 有很灵活和强大的编程接口，同时又有很友好的用户界面。为什么要用 BASH 来代替 sh 呢？Bourne Shell 最大的缺点在于它处理用户的输入方面，在 Bourne shell 里键入命令会很麻烦，尤其当你键入很多相似的命令时，而 BASH 准备了几种特性使命令的输入变得更容易。 BASH 的新功能包括 命令补齐、通配符、命令历史记录、别名等。</p>
<p style="COLOR: rgb(0,255,255)">BASH 是一种解释执行的语言，我们还见过其他一些解释性的语言，如 BASIC 语言、Perl 语言、TCL/TL 等等。解释执行的语言的与编译型语言（如 C 语言）的最大不同就在于运行速度和使用方便程度上。BASH 和后面我们要讲解的 Perl 语言都是解释性语言，他们编写起来很方便，也很快捷，但是由于是解释执行，所以运行速度和效率必将大打折扣。故而今天介绍的 BASH 和 Perl 这两种解释性语言最好的用途就是一些用于完成特定功能的常用的小工具或小程序，对于一些大型的项目、计算复杂的工程或有高级需求的应用还是用 C 语言甚至汇编语言比较好。</p>
<p style="COLOR: rgb(0,255,255)">下面给出一些 BASH 所不适用的范围：</p>
<p style="COLOR: rgb(0,255,255)">资源型敏感，对 CPU 负担重的程序<span class=Apple-converted-space>&nbsp;</span><br>复杂的大项目<span class=Apple-converted-space>&nbsp;</span><br>需要灵活处理文件的程序，BASH 只能逐行读出文件进行处理<span class=Apple-converted-space>&nbsp;</span><br>需要图形用户界面的程序<br>需要直接与系统硬件打交道的程序<span class=Apple-converted-space>&nbsp;</span><br>需要访问 I/O 端口和网络套接字的程序<br>需要使用库或和以前的其他代码整合的程序</p>
<p style="COLOR: rgb(0,255,255)">如果遇到上面这些程序，我们推荐最好用其他编程语言，如：Perl, Tcl, Python 或者直接用 C, C++, Java 。</p>
<p style="COLOR: rgb(0,255,255)">&nbsp;</p>
<p style="COLOR: rgb(0,255,255)"><br><br></p>
<p style="COLOR: rgb(0,255,255)" align=center>&nbsp;</p>
<p style="COLOR: rgb(0,255,255)"><br><br></p>
<a style="COLOR: rgb(0,184,255)" href="http://www.cnitblog.com/cygw/admin/bash-syntax.html"></a><span class=Apple-converted-space>&nbsp;</span><a style="COLOR: rgb(255,51,102)" href="http://www.cnitblog.com/cygw/admin/index.html"></a></div>
</span></div>
<img src ="http://www.cnitblog.com/cygw/aggbug/50287.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/cygw/" target="_blank">cygw</a> 2008-10-15 21:36 <a href="http://www.cnitblog.com/cygw/articles/50287.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>