服务器维护,服务器代维,安全设置,漏洞扫描,入侵检测服务

dirtysea 发表于 2012-10-23 14:22:41

终极Shell——Zsh

<P>[撰文/Kardinal]
<BLOCKQUOTE>子曾经曰过,zsh: The last shell you’ll ever need! Z是最后一个字母,所以它是终极Shell。
<P></P>
<P>我曾经搜索到一个比较各种Shell的文章,Zsh交互性是A+级别的,远高于其它Shell。在编程方面,Zsh是A级的吧,也是最高的。只是不知道出于什么原因,Zsh被严重的低估了。</P></BLOCKQUOTE><SPAN id=more-2227></SPAN>
<P></P>
<P>大多数的Linux用户比较偏爱Bash,因为大多数的发行版默认的就是它。平心而论,Bash确实比Csh之流的好用多了。不过Bash也有很多地方不尽人意,像自动补全的功能不够强大,定位较长路径不够方便等。</P>
<P>后来我使用Zsh。如果不调整一些必要的配置的话,Zsh甚至还不如Bash好用。这也是很多人尝试过并放弃过的原因。</P>
<P><A href="http://i.linuxtoy.org/i/2008/08/zshrctar.gz"><FONT color=#bb5500>Zsh配置文件试用</FONT></A>&nbsp;(内附讲解)</P>
<P>不熟悉Zsh的人,对Zsh最深刻的印象应该就是它的命令提示符了。它支持右侧对齐的提示符,并且可以配置成这个样子的:</P>
<P><A href="http://i.linuxtoy.org/i/2008/08/zsh1.gif"><IMG alt=zsh1-thumb.gif src="http://i.linuxtoy.org/i/2008/08/zsh1-thumb.gif"></A></P>
<P>不过我还是喜欢比较简单的样式。</P>
<P>Zsh的<STRONG>自动补全功能</STRONG>十分的强大,如图所示:</P>
<P><A href="http://i.linuxtoy.org/i/2008/08/zsh2.gif"><IMG alt=zsh2-thumb.gif src="http://i.linuxtoy.org/i/2008/08/zsh2-thumb.gif"></A></P>
<P>它可以自动补全命令、参数、文件名、进程、用户名、变量、权限符等。</P>
<P>Zsh还有一个贴心的功能:<STRONG>路径别名</STRONG>。假设有一个很长的路径,例如/home/lighttpd/html,可以把这个路径命名为 ~WWW。</P>
<P><A href="http://i.linuxtoy.org/i/2008/08/zsh3.gif"><IMG alt=zsh3-thumb.gif src="http://i.linuxtoy.org/i/2008/08/zsh3-thumb.gif"></A></P>
<P>Zsh可以使用<STRONG>Emacs风格的键绑定</STRONG>,习惯Bash键绑定的朋友无需重新适应。Zsh兼容大多数主流Shell,像Bash、Csh等。</P>
<P><STRONG>错误校正</STRONG></P>
<P><STRONG><IMG alt="" src="http://i.linuxtoy.org/i/2008/08/crct11.jpg"></STRONG>-- directory -- 是补全类型提示
<DIV>/etc/x11 后被修正为 /etc/X11</DIV>
<DIV><IMG alt="" src="http://i.linuxtoy.org/i/2008/08/crct21.jpg"> 补全类型提示变成了 -- corrections --</DIV>
<DIV>&nbsp;</DIV>
<DIV>请注意,这个功能不是单纯的修正大小写,而是各种拼写错误比如说上面的例子,如果输入的是11或者s11,它一样会修正为X11</DIV>有一个前提,就是每次修正,只允许有一处字符错误两个以上的错误,除非可以匹配其它的选项,否则就不能修正 12 就不能修正为X11 ,除非候选里有 X12、Y12、Z12……
<DIV>&nbsp;</DIV>
<DIV>在配置文件里找到这一行,修改容错字数</DIV>
<DIV>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; zstyle ':completion:<EM>:approximate:</EM>' max-errors 1 numeric</DIV>当然可以把容错字数改大一些,不过太大了也没有意义了随便输点什么,就可以匹配所有的,和没有一样
<DIV>&nbsp;</DIV>
<DIV><STRONG>强大的重定向功能</STRONG></DIV>
<DIV>同时重定向stdout和stderr到file: command |&amp; &gt;file 同时重定向到多个文件: command &gt;file.1 &gt;file.2</DIV>比如装系统的时候,可以用这个命令
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN><STRONG></STRONG></SPAN></TD></TR>
<TR>
<TD>&nbsp;blkid &gt;&gt; /boot/grub/menu.lst &gt;&gt; /etc/fstab</TD></TR></TBODY></TABLE>
<DIV>&nbsp;</DIV>
<DIV><STRONG>补全类型控制</STRONG></DIV>
<DIV>例如: compctl -g '<EM>.tar.gz *.gz</EM>.tgz' + -g '*(-/)' tar zxvf 过滤候选项</DIV>tar zxvf 候选菜单中只出现扩展名为 .tar.gz .gz .tgz 的文件
<P></P>
<P>不过这个功能比较复杂,容易引起混乱,通常需要脚本配合
<TABLE cellSpacing=1 cellPadding=3 width="90%" align=center border=0>
<TBODY>
<TR>
<TD><SPAN><STRONG>代码:</STRONG></SPAN></TD></TR>
<TR>
<TD>compctl -g '<EM>.tar.bz2 *.tar.gz *.bz2 *.gz *.jar *.rar *.tar *.tbz2 *.tgz *.zip *.Z' + -g '</EM>(-/)' extractextract() { &nbsp; &nbsp;if [[ -z "$1" ]] ; then &nbsp; &nbsp; &nbsp; &nbsp;print -P "usage: \e] ; then &nbsp; &nbsp; &nbsp; &nbsp;case ${(L)1} in &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.tar.bz2)&nbsp; tar -jxvf $1&nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>.tar.gz)&nbsp; &nbsp;tar -zxvf $1&nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.bz2)&nbsp; &nbsp; &nbsp; bunzip2 $1&nbsp; &nbsp; &nbsp; &nbsp;;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>.gz)&nbsp; &nbsp; &nbsp; &nbsp;gunzip $1&nbsp; &nbsp; &nbsp; &nbsp;;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.jar)&nbsp; &nbsp; &nbsp; unzip $1&nbsp; &nbsp; &nbsp; &nbsp;;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>.rar)&nbsp; &nbsp; &nbsp; unrar x $1&nbsp; &nbsp; &nbsp; &nbsp;;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.tar)&nbsp; &nbsp; &nbsp; tar -xvf $1&nbsp; &nbsp; &nbsp; &nbsp;;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>.tbz2)&nbsp; &nbsp; &nbsp;tar -jxvf $1&nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.tgz)&nbsp; &nbsp; &nbsp; tar -zxvf $1&nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>.zip)&nbsp; &nbsp; &nbsp; unzip $1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<EM>.Z)&nbsp; &nbsp; &nbsp; &nbsp; uncompress $1&nbsp; &nbsp; ;; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</EM>)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "Unable to extract '$1' :: Unknown extension" &nbsp; &nbsp; &nbsp; &nbsp;esac &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp;echo "File ('$1') does not exist!" &nbsp; &nbsp;fi }</TD></TR></TBODY></TABLE><SPAN>考虑到使用的不多,配置又麻烦,我没有配置这个功能不过我想肯定有人愿意在这上面花点时间</SPAN>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><STRONG>将Zsh设置为默认Shell</STRONG>(不建议更改root用户的默认shell)</DIV>
<DIV>usermod -s /usr/local/bin/<SPAN>zsh</SPAN></DIV>
页: [1]
查看完整版本: 终极Shell——Zsh