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

📄 详解bash命令行处理 linuxsir_org.htm

📁 linuxSir 网站的精华文章
💻 HTM
📖 第 1 页 / 共 2 页
字号:
    |                                 ||                          |     |
    |                                 \/                          |     |
    |                           +--------------+                  |     |
    |                           |  9.单词分割  |                  |     |
    |                           +--------------+                  |     |
    |                                 ||                          |     |
    |                                 \/                          |     |
    |                           +--------------+                  |     |
    |                           | 10.路径名扩展|                  |     |
    |                           +--------------+                  |     |
    |                                 ||                          |     |
    |                                 \/                          |     |
    |               +----------------------------------------+    |     |
    |               | 11.命令查寻:函数,内置命令,可执行文件|<---|-----|
    |               +----------------------------------------+
    |                                 ||
    |                                 \/
    |将参数带入下一个命令        +-------------+
    |----------eval--------------| 12.运行命令 |
                                 +-------------+
</PRE>
      <P>结合上面的插图,这里给出命令行的12个步骤。<BR><B><BR>1、将命令行分成由固定元字符集分隔的记号;<BR></B><BR>
      <DIV class=codeblock><CODE>SPACE, TAB, NEWLINE, ; , (, ), &lt;, &gt;, |, 
      &amp;</CODE></DIV><BR>记号类型包括单词,关键字,I/O重定向符和分号。<BR><B><BR>2、检测每个命令的第一个记号,查看是否为不带引号或反斜线的关键字。<BR></B><BR>如果是一个开放的关键字,如if和其他控制结构起始字符串,function,{或(,则命令实际上为一复合命令。shell在内部对复合命令进行处理,读取下一个命令,并重复这一过程。如果关键字不是复合命令起始字符串(如then等一个控制结构中间出现的关键字),则给出语法错误信号。<BR><B><BR>3、依据别名列表检查每个命令的第一个关键字;<BR></B><BR>如果找到相应匹配,则替换其别名定义,并退回第一步;否则进入第4步。该策略允许递归别名,还允许定义关键字别名。如alias 
      procedure=function<BR><B><BR>4、执行大括号扩展,例如a{b,c}变成ab 
      ac<BR></B><BR><B><BR>5、如果~位于单词开头,用$HOME替换~。<BR></B><BR>使用usr的主目录替换~user。<BR><B><BR>6、对任何以符号$开头的表达式执行参数(变量)替换;<BR></B><BR><B><BR>7、对形式$(string)的表达式进行命令替换;<BR></B><BR>这里是嵌套的命令行处理。<BR><B><BR>8、计算形式为$((string))的算术表达式;<BR></B><BR><B><BR>9、把行的参数,命令和算术替换部分再次分成单词,这次它使用$IFS中的字符做分割符而不是步骤1的元字符集;
      <P></P>
      <P>10、对出现*, ?, [ / ]对执行路径名扩展,也称为通配符扩展;</P>
      <P>11、按命令优先级表(跳过别名),进行命令查寻;</P>
      <P>12、设置完I/O重定向和其他操作后执行该命令。<BR></B></P>
      <P><FONT id=2 size=4><B>二、关于引用</B></FONT></P>
      <P>1、单引号跳过了前10个步骤,不能在单引号里放单引号<BR>2、双引号跳过了步骤1~5,步骤9~10,也就是说,只处理6~8个步骤。</P>
      <P>也就是说,双引号忽略了管道字符,别名,~替换,通配符扩展,和通过分隔符分裂成单词。<BR>双引号里的单引号没有作用,但双引号允许参数替换,命令替换和算术表达式求值。可以在双引号里包含双引号,方式是加上转义符"\",还必须转义$, 
      `, \。</P>
      <P><FONT id=3 size=4><B>三、eval的作用;</B></FONT> </P>
      <P>eval的作用是再次执行命令行处理,也就是说,对一个命令行,执行两次命令行处理。这个命令要用好,就要费一定的功夫。我举两个例子,抛砖引玉。</P>
      <P><FONT id=3.1 size=3><B>1、例子1:用eval技巧实现shell的控制结构for</B></FONT></P>
      <P>用eval技巧实现shell的控制结构for。</P>
      <P>
      <DIV class=codeblock><CODE>[root@home root]# cat 
      myscript1<BR>#!/bin/sh<BR>evalit(){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if 
      [ $cnt = 1 
      ];then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval 
      $@<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;let 
      cnt=cnt-1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evalit 
      $@<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fi<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval 
      $@<BR>}<BR>cnt=$1<BR>echo $cnt | egrep "^[1-9][0-9]*$" &gt;/dev/null<BR>if 
      [ $? -eq 0 ]; 
      then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shift<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;evalit 
      $@<BR>else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo 
      'ERROR!!! Check your input!'<BR>fi<BR>[root@home root]# ./myscript1 3 
      hostname<BR>home<BR>home<BR>home<BR>[root@home root]# ./myscript1 5 id 
      |cut -f1 -d' 
      '<BR>uid=0(root)<BR>uid=0(root)<BR>uid=0(root)<BR>uid=0(root)<BR>uid=0(root)</CODE></DIV><BR><B>注意:</B>bash里有两个很特殊的变量,它们保存了参数列表。
      <P></P>
      <P>$*,保存了以$IFS指定的分割符所分割的字符串组。<BR>$@,原样保存了参数列表,也就是"$1""$2"...</P>
      <P>这里我使用了函数递归以及eval实现了for结构。<BR>当执行eval $@时,它经历了步骤如下:<BR>第1步,分割成eval 
      $@<BR>第6步,扩展$@为hostname<BR>第11步,找到内置命令eval<BR>重复一次命令行处理,第11步,找到hostname命令,执行。</P>
      <P><B>注意:</B>也许有人想当然地认为,何必用eval呢?直接$@来执行命令就可以了嘛。</P>
      <P><FONT id=3.2 size=3><B>例子2:一个典型错误的例子</B></FONT></P>
      <P>错误!这里给个典型的例子大家看看。</P>
      <P>
      <DIV class=codeblock><CODE>[root@home root]# a="id | cut -f1 -d' 
      '"<BR>[root@home root]# $a<BR>id:无效选项 -- f<BR>请尝试执行‘id 
      --help’来获取更多信息。<BR>[root@home root]# eval 
      $a<BR>uid=0(root)</CODE></DIV><BR>如果命令行复杂的话(包括管道或者其他字符),直接执行$a字符串的内容就会出错。分析如下。<BR>$a的处理位于第6步──参数扩展,也就是说,跳过了管道分析,于是"|", 
      "cut", "-f1", "-d"都变成了id命令的参数,当然就出错啦。<BR>但使用了eval,它把第一遍命令行处理所得的"id", "|", 
      "cut", "-f1", "-d"这些字符串再次进行命令行处理,这次就能正确分析其中的管道了。
      <P></P>
      <P><B>总而言之:</B>要保证你的命令或脚本设计能正确通过命令行处理,跳过任意一步,都可能造成意料外的错误!</P>
      <P><FONT id=3.3 size=3><B>例子3:设置系统的ls色彩显示</B></FONT></P>
      <P>
      <DIV class=codeblock><CODE>eval $(dircolors -b 
      /etc/dircolors)</CODE></DIV><BR>eval语句通知shell接受eval参数,并再次通过命令行处理的所有步骤运行它们。<BR>它使你可以编写脚本随意创建命令字符串,然后把它们传递给shell执行;<BR>$()是命令替换,返回命令的输出字符串。<BR>其中dircolors命令根据/etc/dircolors配置文件生成设置环境变量LS_COLORS的bash代码,内容如下<BR>
      <DIV class=codeblock><CODE>[root@localhost root]# dircolors -b &gt; 
      tmp<BR>[root@localhost root]# cat 
      tmp<BR>LS_COLORS='no=00:fi=00:di=01;34:ln=01; ......<BR>export 
      LS_COLORS<BR>#这里我没有指定配置文件,所以dircolors按预置数据库生成代码。<BR>其输出被eval命令传递给shell执行。</CODE></DIV><BR>eval是对Bash 
      Shell命令行处理规则的灵活应用,进而构造"智能"命令实现复杂的功能。<BR>上面提及的命令是eval其中一个很普通的应用,它重复了1次命令行参数传递过程,纯粹地执行命令的命令。<BR>其实它是bash的难点,是高级bash程序员的必修之技。<BR><BR>
      <P></P>
      <P><FONT id=4 
      size=4><B>四、命令优先级表</B></FONT><BR>1、别名<BR>2、关键字<BR>3、函数<BR>4、内置命令<BR>5、脚本或可执行程序($PATH)<BR><BR><BR><FONT 
      id=5 size=4><B><BR>五、鉴于一些学习中会遇到的困惑,我再给出一些有趣的命令。<BR></B></FONT></P>
      <P><FONT id=5.1 size=3><B>1、command builtin enable</B></FONT></P>
      <P>上面的命令行提及过,第11步会进行命令查找,那它的具体过程如何呢?<BR>它的默认查找次序为函数,内部命令,脚本和可执行代码。我们往往要在实际编程中跳过一些查找项以满足一定的功能需求。这时候就要用到这三个命令来施展魔法~~</P>
      <P><FONT id=5.2 size=3><B>2、command</B></FONT></P>
      <P>跳过别名和函数的查找,换句话说,它只查找内部命令以及搜索路径中找到的脚本或可执行程序。<BR>这里举个有趣的例子。</P>
      <P>
      <DIV class=codeblock><CODE>[root@home root]# type -all pwd<BR>pwd is a 
      shell builtin<BR>pwd is /bin/pwd<BR>[root@home root]# cat 
      myscript2<BR>#!/bin/sh<BR>pwd(){<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo 
      "This is the current 
      directory."<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command 
      pwd<BR>}<BR>pwd<BR>[root@home root]# ./myscript2<BR>This is the current 
      directory.<BR>/root</CODE></DIV>
      <P></P>
      <P>我用pwd()函数取代了内置命令pwd以及外部命令/bin/pwd,然后在脚本里执行内置命令pwd。在这里我们为什么要用command呢?是为了避免函数陷入递归循环,因为函数名与内置命令同名,而函数的优先级比内置命令高。</P>
      <P><FONT id=5.3 size=3><B>3、builtin</B></FONT></P>
      <P>顾名思义,它只查找内置命令。这个命令很简单,就不多说了。</P>
      <P><FONT id=5.4 size=3><B>4、enable</B></FONT></P>
      <P>与builtin相反,它屏蔽一个内置命令,允许运行一个shell脚本或同名的可执行代码而无须给出完全路径名。<BR>举个例子吧。</P>
      <P>pwd命令有两个,一个是shell内置的,一个是可执行程序。</P>
      <P>当执行一些奇怪的路径名后,shell内置的pwd会打印出"错误信息",但外部的pwd会打印出当前目录的"原来面目"。请看下面:<BR>
      <DIV class=codeblock><CODE>[root@home root]# cd //<BR>[root@home //]# 
      pwd<BR>//<BR>[root@home //]# type -all pwd<BR>pwd is a shell 
      builtin<BR>pwd is /bin/pwd<BR>[root@home //]# /bin/pwd<BR>/<BR>[root@home 
      //]# enable -n pwd<BR>[root@home //]# pwd<BR>/</CODE></DIV>
      <P></P>
      <P>这样,用enable -n屏蔽内置pwd命令后,就可以用外部pwd打印出正确的路径名了。</P>
      <P>Bash博大精深,希望大家好好学习。:)</P>
      <P><FONT id=6 size=4><B>六、关于本文</B></FONT></P>
      <P>本文是home_king兄发在LinuxSir.Org 讨论区的一个专题<A 
      href="http://www.linuxsir.org/bbs/showthread.php?t=99465">《 
      【Bas命令行处理】[详解]》</A> ,我看这篇文档写的很不错,适用新手,就整理出来了,并对段落进行了相应的排版和格式化,以方便大家阅读;</P>
      <P><FONT id=7 size=4 <b>七、相关文档</B></FONT></P></DIV>
      <DIV class=links>By 北南南北 at 2005/12/18 - 16:41 | <A 
      href="http://www.linuxsir.org/main/?q=taxonomy/term/1">Linux</A> | <A 
      href="http://www.linuxsir.org/main/?q=taxonomy/term/52">命令/SHELL/PERL</A> 
      | <A title=共享你有关本文的思想和意见。 
      href="http://www.linuxsir.org/main/?q=comment/reply/134#comment">参与评论</A> 
      | 4969 阅读</DIV></DIV><A id=comment></A>
      <FORM action=?q=comment method=post>
      <DIV><INPUT type=hidden value=134 name=edit[nid]> </DIV></FORM><!-- end content -->
      <DIV id=footer>
      <CENTER><A href="http://www.linuxsir.org/"><IMG 
      src="详解Bash命令行处理  LinuxSir_Org.files/logo.jpg"></A> <BR><A 
      href="http://www.miibeian.gov.cn/" target=_blank><FONT color=blue 
      size=3><B>闽ICP备06025536号</B></FONT></A><BR>
      <SCRIPT language=JavaScript 
      src="详解Bash命令行处理  LinuxSir_Org.files/cyberpolice.htm"></SCRIPT>
      <BR><A href="http://www.linuxsir.org/main/?q=node/78"><FONT color=blue 
      size=3><B>© 2002-2006 LinuxSir.Org</B></FONT></A><BR></CENTER></DIV></TD>
    <TD id=sidebar-right>
      <DIV class="block block-block" id=block-block-2>
      <H2 class=title>基础知识</H2>
      <DIV class=content>
      <UL>
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/23">安装配置</A> 
        <LI><A 
        href="http://www.linuxsir.org/main/?q=taxonomy/term/1/25/">基础入门</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/22/">硬件解决</A> 

        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/21">软件管理</A> 
        <LI><A 
        href="http://www.linuxsir.org/main/?q=taxonomy/term/35/48/">重要资源</A> 
        </LI></UL></DIV></DIV>
      <DIV class="block block-block" id=block-block-4>
      <H2 class=title>软件应用</H2>
      <DIV class=content>
      <UL>
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/30">网络工具</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/32">图形图像</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/31">音乐视频</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/49">字体中文</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/33">软件其它</A> 
        </LI></UL></DIV></DIV>
      <DIV class="block block-block" id=block-block-5>
      <H2 class=title>网络服务器</H2>
      <DIV class=content>
      <UL>
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/27">文件服务器<A> 
        <LI><A 
        href="http://www.linuxsir.org/main/?q=taxonomy/term/28">Web服务器</A> 
        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/29">邮件服务器</A> 

        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/37">数据库应用</A> 

        <LI><A href="http://www.linuxsir.org/main/?q=taxonomy/term/50">服务器其它</A> 
        </LI></UL></DIV></DIV>
      <DIV class="block block-comment" id=block-comment-0>
      <H2 class=title>最新评论</H2>
      <DIV class=content>
      <DIV class=item-list>
      <UL>
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/222#comment-727">修改主机名不是修改</A><BR>11 
        min 20 sec 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/236#comment-726">望远镜</A><BR>1 
        day 5 hours 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/145#comment-725">我也遇到isql不能支持中文的问题</A><BR>1 
        day 23 hours 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/137#comment-724">很好的文章</A><BR>2 
        days 2 hours 前
        <LI><A href="http://www.linuxsir.org/main/?q=node/78#comment-723">支持... 
        ....</A><BR>2 days 10 hours 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/228#comment-722">Congratulations</A><BR>2 
        days 13 hours 前
        <LI><A href="http://www.linuxsir.org/main/?q=node/236#comment-721">不能, 
        vsftpd</A><BR>3 days 3 hours 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/140#comment-720">谢谢!!</A><BR>3 
        days 11 hours 前
        <LI><A href="http://www.linuxsir.org/main/?q=node/158#comment-719">To 
        solve the issue:yum</A><BR>3 days 16 hours 前
        <LI><A 
        href="http://www.linuxsir.org/main/?q=node/137#comment-718"></A><BR>5 
        days 44 min 
前</LI></UL></DIV></DIV></DIV></TD></TR></TBODY></TABLE></BODY></HTML>

⌨️ 快捷键说明

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