SHELL彩色的命令行

sunxphere posted @ 2010年3月12日 00:39 , 1606 阅读
<table><tbody><tr><td align="center" height="25"><br></td> </tr> <tr> <td align="center" height="9"><img style="width:245px;height:4px" alt="" src="http://blog.chinaunix.net/templates/default/images/right_line.gif" border="0" height="9" width="502"></td> </tr> <tr> <td align="center"> <table style="border-collapse:collapse" border="0" cellpadding="0" cellspacing="0" width="740"> <tbody> <tr> <td width="740"> <div style="margin:15px" width="560"> <table style="border-collapse:collapse" bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" width="760"> <tbody> <tr> <td align="center" height="25"><font style="font-size:14pt" color="#02368d"><b>彩色的命令行</b></font><br></td> </tr> <tr> <td align="center" height="9"><br></td> </tr> <tr> <td align="center"> <table style="border-collapse:collapse" border="0" cellpadding="0" cellspacing="0" width="740"> <tbody> <tr> <td width="740"> <div style="margin:15px;line-height:150%" width="560"> <div><span> <h2>概述</h2> 本文以 Bash shell 为例。(不同的终端里的转义序列不完全相同). <h2>Shell 的配置</h2> shell 彩色配置出现在 bash 的个人配置文件 <i>~/.bashrc</i> 或者是全局配置文件 <i>/etc/bashrc</i> 里面。 可以通过 <i>bashrc</i> 里面的 PS1 变量来设置提示符的外观。 <br><br>例如: <table> <tbody> <tr> <td> <pre>PS1=&quot;\s-\v\$ &quot;</pre> <pre>\s 表示 shell 的名称,-\v 表示版本号。 在提示符的最后面放置了一个 $。</pre> <pre>PS1=&quot;\u@\h \w \$ &quot;</pre> </td> </tr> </tbody> </table> 表示 用户@ 当前目录 $ <h2>转义序列</h2> 要是通过彩色化提示符来增加个性化,就要用到转义序列。 转义序列就是一个让 shell 执行一个特殊步骤的控制指令。转义序列通常都是以 ESC 开头(这也是它的命名原因)。 在 shell 里表示为 ^[。也可以用 \033 完成相同的工作(ESC 的 ASCII 码用十进制表示就是 27,  用八进制表示为 33)。<br><br>要直接在 shell 里面输入转义序列需要先按 ctrl-v:<i>CTRL-v ESC</i>。 <a name="335lfindex3"> </a> <h2>使用 shell 的颜色</h2> 下面用一个提示符的例子来解释 shell 的颜色。 <pre>PS1=&quot;\[\033[0;32;40m\u@\h:\w\$ \]&quot;</pre> <pre>这样提示符就全部显示成绿色了。就像这样:<br></pre> <center> <table border="0" cellpadding="10"> <tbody> <tr> <td bgcolor="#000000"><font color="#00ff00">nico@ebrain:~$</font> </td> </tr> </tbody> </table> </center> <p><br>\033 声明了转义序列的开始,然后是 [ 开始定义颜色。 后面的 0 定义了默认的字体宽度。稍后我会介绍其他的可用字符。 转义序列字符串要用 \[ 和 \] 括起来, 防止转义序列的文本显示在 shell 里占用太多的空间。<br><br>下面要选择前景色(这里是 32,代表绿色)。背景色的 40 表示黑色。 要是不想让提示符后面的文字也变成绿色,需要用 \033[0m 关闭转义序列, \033[0m 是 shell 的默认颜色。前景色和背景色都有 8 种可用的选择。<br><br>可选颜色:红色、绿色、黄色、蓝色、洋红、青色和白色。 他们对应的颜色代码是:30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)。<br><br>用同样色方法设置背景色,不过要把第一个数字“3”替换成“4”, 例如 40、41、42、43、44、45、46、47。<br><br>示例: </p> <pre>PS1=&quot;\[\033[0;37;44m\u@\033[0;32;43m\h:\033[0;33;41m\w$\033[0m\]&quot;<br>这给了非常多彩的提示符:<br></pre> <center> <table border="0" cellpadding="10"> <tbody> <tr> <td bgcolor="#000000"> <table border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td bgcolor="#0000ff"><font color="#ffffff">nico@</font></td> <td bgcolor="#aa5500"><font color="#00ff00">ebrain:</font></td> <td bgcolor="#ff3030"><font color="#aa5500">~$</font></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </center><br>使用 <i>export PS1=&quot;string&quot;</i> 来测试这些设置; <pre>PS1=&quot;\[\033[1;34;40m[\033[1;31;40m\u@\h:\w\033[1;34;40m]\033[1;37;40m $\033[0;37;0m\] &quot;<br></pre> <br><center> <table border="0" cellpadding="10"> <tbody> <tr> <td bgcolor="#000000"><b><font color="#0000ff">[</font> <font color="#0000ff">nico@ebrain:~</font> <font color="#0000ff">]</font></b> </td> </tr> </tbody> </table> </center><a name="335lfindex4"> </a> <h2>文本属性</h2> <p>刚才提到了,在第一个转义序列后面的“0”是提示符的文本的默认颜色设置。 对于文本属性来说,有意义的值及对应关系为:<br>0、默认值<br>1、粗体<br>22、非粗体<br>4、下划线<br>24、非下划线<br>5、闪烁<br>25、非闪烁<br>7、反显<br>27、非反显</p> <p>通过下面这一段短小的脚本,可以看看色彩组合。 </p> <pre>#!/bin/sh<br>############################################################<br># Nico Golde &lt;nico(at)<a href="http://ngolde.de">ngolde.de</a>&gt; Homepage: <a href="http://www.ngolde.de">http://www.ngolde.de</a><br># Last change: Mon Feb 16 16:24:41 CET 2004<br>############################################################<br><br>for attr in 0 1 4 5 7 ; do<br> echo &quot;----------------------------------------------------------------&quot;<br> printf &quot;ESC[%s;Foreground;Background - \n&quot; $attr<br> for fore in 30 31 32 33 34 35 36 37; do<br> for back in 40 41 42 43 44 45 46 47; do<br> printf &#39;\033[%s;%s;%sm s;s &#39; $attr $fore $back $fore $back<br> done<br> printf &#39;\n&#39;<br> done<br> printf &#39;\033[0m&#39;<br>done<br></pre> <h2>另一个程序</h2> 在 shell 中设置颜色不仅仅能创建更加漂亮的提示符, 在编写控制台程序的时候也非常有用。<br><br>对于一个要使用彩色的程序员,就必须要使用类似 <i>slang</i> 或 <i>ncurses</i> 这样的的库,者通常会增加执行文件的大小。 <i>Ncurses</i> 有着或多或少的独立于终端的类型的优势。 <a name="335lfindex6"> </a> <h2>C 语言示例</h2> 用绿色打印“Hello Word”: <pre>#i nclude &lt;stdio.h&gt;<br>int main(void){<br> const char *const green = &quot;\033[0;40;32m&quot;;<br> const char *const normal = &quot;\033[0m&quot;;<br> printf(&quot;%sHello World%s\n&quot;, green, normal);<br> return 0;<br>}<br></pre> 另外一个有用的转义序列是 <i>printf(&quot;\033[2J&quot;)</i>,它和 <i>system(clear)</i> 完成的功能一样。但是可以不需要头文件 <i>unistd.h</i>。<br><br>使用<i>printf(&quot;\033[1K&quot;)</i> 可以删除一行。 <a name="335lfindex7"> </a> <h2>初始化脚本示例</h2> 如果想在 <i>/etc/init.d</i> 的 <i>init</i> 脚本成功的执行后, 得到一个漂亮的、清晰易读的提示,而不是一个简单的 <i>&#39;.&#39;</i>, 可以再一次的使用转义序列。<br><br>这是一段 <i>cron init script</i> 的摘录: <pre>#!/bin/sh<br># Start/stop the cron daemon.<br>test -f /usr/sbin/cron || exit 0<br><br> case &quot;$1&quot; in<br> start) echo -n &quot;Starting periodic command scheduler: cron&quot;<br> start-stop-daemon --start --quiet --exec /usr/sbin/cron<br><br> echo &quot;.&quot;<br>;;<br><br></pre> 如果 <i>cron</i> 脚本执行成功就会显示一个句点。 可以用 [Ok] 给这些信息添加上彩色特征,通过改变 <i>echo</i> 字符串,例如: <pre>#!/bin/sh<br># Start/stop the cron daemon.<br>test -f /usr/sbin/cron || exit 0<br>case &quot;$1&quot; in<br>start) echo -n &quot;Starting periodic command scheduler: cron&quot;<br> start-stop-daemon --start --quiet --exec /usr/sbin/cron<br>echo &quot;\[ \033[1;34;40m[ \033[1;32;40mOk \033[1;34;40m]\033[0m\]&quot;<br> ;;<br></pre> <p>把这个设置应用到所有的 <i>init</i>脚本上非常耗费时间, 除非使用转义序列 \033 —— 因为 <i>Ctrl-v</i> 不是作为一个字符来处理的。 <a name="335lfindex8"> </a></p> <p> </p> <p><b>定制命令提示符</b></p> <p>我们可以定制要显示的bash命令提示符,包括当前用户名和主机名,当前时间,平均负载和当前工作目录。要实现该目的,修改$PS1变量,如下所示:</p> <p>bash&gt; PS1=&#39;u@h:w @&gt; &#39;<br>bash&gt; export PS1<br>root@medusa:/tmp 03:01 PM&gt;</p> <p>结果将会在命令行显示当前登录的用户名、主机名、当前工作目录和当前时间。从用户指南页可以获得bash可以理解的符号列表。</p> </span></div> </div></td></tr></tbody></table></td></tr></tbody></table></div></td></tr></tbody></table></td></tr></tbody></table>
  • 无匹配
  • 无匹配

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter