本文链接:http://codingstandards.iteye.com/blog/796598 (转载请注明出处)
在使用bash命令行时,在提示符下,输入某个命令的前面几个字符,然后按TAB键,就会列出以这几个字符开头的命令供我们选择。不光如此,还可以进行参数补全,但只限于文件参数,当输入到参数部分时,按TAB键,就会列出以这个参数开头的文件路径供我们选择。Bash提供了一个complete内建命令,它的用途是规定参数怎么自动补全(Specify how arguments to each name should be completed.)。它怎么使用在网上的资料都没怎么看见,man手册页不太想看,但是这个世界好人真的挺多,网站http://www.caliban.org/bash/ 提供了一个称之为bash-completion的软件包,通过一个复杂的脚本bash_completion实现可编程的补全程序,减少系统管理员日常维护工作,减少差错,提高工作效率。
常用参数
不用管参数了,只管在输入参数时按TAB键补全了。
使用示例
示例一 安装bash-completion的过程
[root@rhel55 ~]# type -a complete
complete is a shell builtin <== complete是一个Bash内建命令
[root@rhel55 ~]#
[root@rhel55 ~]# cd setup/
下面用wget从 http://www.caliban.org/bash/ 下载源代码的rpm包。
[root@rhel55 setup]# wget http://www.caliban.org/files/redhat/SRPMS/bash-completion-20060301-1.src.rpm
--2010-10-28 13:32:20-- http://www.caliban.org/files/redhat/SRPMS/bash-completion-20060301-1.src.rpm
正在解析主机 www.caliban.org... 83.98.221.239
Connecting to www.caliban.org|83.98.221.239|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:170596 (167K) [application/x-rpm]
Saving to: `bash-completion-20060301-1.src.rpm'
100%[==========================================================================================>] 170,596 91.0K/s in 1.8s
2010-10-28 13:32:23 (91.0 KB/s) - `bash-completion-20060301-1.src.rpm' saved [170596/170596]
[root@rhel55 setup]# ls -l bash-completion-20060301-1.src.rpm
-rw-r--r-- 1 root root 170596 2006-03-02 bash-completion-20060301-1.src.rpm
[root@rhel55 setup]# rpmbuild --rebuild bash-completion-20060301-1.src.rpm //yum install rpm-build
Installing bash-completion-20060301-1.src.rpm
warning: InstallSourcePackage: Header V3 DSA signature: NOKEY, key ID bfbaf0e8
warning: user ianmacd does not exist - using root
warning: group ianmacd does not exist - using root
warning: user ianmacd does not exist - using root
warning: group www does not exist - using root
Executing(%prep): /bin/sh -e /var/tmp/rpm-tmp.75172
+ umask 022
+ cd /usr/src/redhat/BUILD
+ LANG=C
+ export LANG
+ unset DISPLAY
+ cd /usr/src/redhat/BUILD
+ rm -rf bash_completion
+ /usr/bin/bzip2 -dc /usr/src/redhat/SOURCES/bash-completion-20060301.tar.bz2
+ tar -xvvf -
drwxrwxr-x ianmacd/ianmacd 0 2006-03-02 00:38:44 bash_completion/
drwxrwxr-x ianmacd/ianmacd 0 2006-03-01 18:45:13 bash_completion/contrib/
-rw-r--r-- ianmacd/ianmacd 748 2005-01-03 10:44:22 bash_completion/contrib/sitecopy
-rw-r--r-- ianmacd/ianmacd 261 2003-12-31 16:22:37 bash_completion/contrib/isql
-rw-r--r-- ianmacd/ianmacd 388 2002-06-13 06:03:23 bash_completion/contrib/bitkeeper
-rw-r--r-- ianmacd/ianmacd 442 2004-03-31 06:39:11 bash_completion/contrib/cksfv
-rw-r--r-- ianmacd/ianmacd 3621 2003-12-31 16:22:37 bash_completion/contrib/dsniff
-rw-r--r-- ianmacd/ianmacd 1254 2003-12-31 16:22:37 bash_completion/contrib/freeciv
-rw-r--r-- ianmacd/ianmacd 659 2003-12-31 16:22:37 bash_completion/contrib/gkrellm
-rw-r--r-- ianmacd/ianmacd 1230 2002-08-20 00:57:44 bash_completion/contrib/gnatmake
-rw-r--r-- ianmacd/ianmacd 258 2003-12-31 16:22:37 bash_completion/contrib/harbour
-rw-r--r-- ianmacd/ianmacd 1838 2003-12-31 16:22:37 bash_completion/contrib/larch
-rw-r--r-- ianmacd/ianmacd 163 2003-12-31 16:22:37 bash_completion/contrib/lilypond
-rw-r--r-- ianmacd/ianmacd 1917 2003-12-31 16:22:37 bash_completion/contrib/mcrypt
-rw-r--r-- ianmacd/ianmacd 1024 2004-01-01 08:54:41 bash_completion/contrib/mtx
-rw-r--r-- ianmacd/ianmacd 1164 2003-12-31 16:22:37 bash_completion/contrib/p4
-rw-r--r-- ianmacd/ianmacd 1696 2003-12-31 16:22:37 bash_completion/contrib/povray
-rw-r--r-- ianmacd/ianmacd 2613 2006-02-25 22:25:59 bash_completion/contrib/ri
-rw-r--r-- ianmacd/ianmacd 491 2003-12-31 16:22:37 bash_completion/contrib/unace
-rw-r--r-- ianmacd/ianmacd 608 2004-07-06 07:37:47 bash_completion/contrib/unrar
-rw-r--r-- ianmacd/ianmacd 406 2005-01-03 11:00:44 bash_completion/contrib/snownews
-rw-r--r-- ianmacd/ianmacd 8673 2004-03-31 07:05:34 bash_completion/contrib/mailman
-rw-r--r-- ianmacd/ianmacd 664 2004-03-31 07:05:45 bash_completion/contrib/sbcl
-rw-r--r-- ianmacd/ianmacd 644 2004-03-31 07:05:45 bash_completion/contrib/lisp
-rw-r--r-- ianmacd/ianmacd 669 2004-03-31 07:05:45 bash_completion/contrib/clisp
-rw-r--r-- ianmacd/ianmacd 603 2004-03-31 07:05:45 bash_completion/contrib/gcl
-rw-r--r-- ianmacd/ianmacd 6701 2006-03-01 18:45:03 bash_completion/contrib/svk
-rw-r--r-- ianmacd/ianmacd 2215 2005-07-12 01:49:14 bash_completion/contrib/modules
-rw-r--r-- ianmacd/ianmacd 1038 2004-10-17 16:22:09 bash_completion/contrib/bittorrent
-rw-r--r-- ianmacd/ianmacd 3019 2006-02-26 08:25:41 bash_completion/contrib/hg
-rw-r--r-- ianmacd/ianmacd 17992 2000-07-12 17:40:03 bash_completion/COPYING
-rw-r--r-- ianmacd/ianmacd 799 2003-05-01 13:49:58 bash_completion/BUGS
-rw-rw-r-- ianmacd/ianmacd 184573 2006-03-02 00:39:58 bash_completion/Changelog
-rw-r--r-- ianmacd/ianmacd 17005 2006-02-25 22:41:11 bash_completion/README
-rw-r--r-- ianmacd/ianmacd 215739 2006-03-02 00:20:18 bash_completion/bash_completion
-rwxr-xr-x ianmacd/ianmacd 557 2006-02-25 09:37:15 bash_completion/bash_completion.sh
-rw-r--r-- ianmacd/ianmacd 2528 2006-03-02 00:19:26 bash_completion/TODO
+ STATUS=0
+ '[' 0 -ne 0 ']'
+ cd bash_completion
++ /usr/bin/id -u
+ '[' 0 = 0 ']'
+ /bin/chown -Rhf root .
++ /usr/bin/id -u
+ '[' 0 = 0 ']'
+ /bin/chgrp -Rhf root .
+ /bin/chmod -Rf a+rX,u+w,g-w,o-w .
+ exit 0
Executing(%install): /bin/sh -e /var/tmp/rpm-tmp.75172
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd bash_completion
+ LANG=C
+ export LANG
+ unset DISPLAY
+ rm -rf /var/tmp/bash-completion-root bash-completion-ghosts.list
+ install -dm 0755 /var/tmp/bash-completion-root/etc/profile.d
+ install -dm 0755 /var/tmp/bash-completion-root/etc/bash_completion.d
+ install -pm 0644 bash_completion /var/tmp/bash-completion-root/etc/
+ sed -e s@/etc@/etc@g /usr/src/redhat/SOURCES/bash_completion.sh
+ touch -r /usr/src/redhat/SOURCES/bash_completion.sh /var/tmp/bash-completion-root/etc/profile.d/bash_completion.sh
+ install -dm 755 /var/tmp/bash-completion-root/usr/share/bash-completion
+ install -pm 644 contrib/bitkeeper contrib/bittorrent contrib/cksfv contrib/clisp contrib/dsniff contrib/freeciv contrib/gcl contrib/gkrellm contrib/gnatmake contrib/harbour contrib/hg contrib/isql contrib/larch contrib/lilypond contrib/lisp contrib/mailman contrib/mcrypt contrib/modules contrib/mtx contrib/p4 contrib/povray contrib/ri contrib/sbcl contrib/sitecopy contrib/snownews contrib/svk contrib/unace contrib/unrar /var/tmp/bash-completion-root/usr/share/bash-completion
+ cd contrib
+ for f in '*'
+ ln -s /usr/share/bash-completion/bitkeeper /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/bitkeeper'
+ for f in '*'
+ ln -s /usr/share/bash-completion/bittorrent /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/bittorrent'
+ for f in '*'
+ ln -s /usr/share/bash-completion/cksfv /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/cksfv'
+ for f in '*'
+ ln -s /usr/share/bash-completion/clisp /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/clisp'
+ for f in '*'
+ ln -s /usr/share/bash-completion/dsniff /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/dsniff'
+ for f in '*'
+ ln -s /usr/share/bash-completion/freeciv /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/freeciv'
+ for f in '*'
+ ln -s /usr/share/bash-completion/gcl /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/gcl'
+ for f in '*'
+ ln -s /usr/share/bash-completion/gkrellm /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/gkrellm'
+ for f in '*'
+ ln -s /usr/share/bash-completion/gnatmake /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/gnatmake'
+ for f in '*'
+ ln -s /usr/share/bash-completion/harbour /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/harbour'
+ for f in '*'
+ ln -s /usr/share/bash-completion/hg /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/hg'
+ for f in '*'
+ ln -s /usr/share/bash-completion/isql /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/isql'
+ for f in '*'
+ ln -s /usr/share/bash-completion/larch /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/larch'
+ for f in '*'
+ ln -s /usr/share/bash-completion/lilypond /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/lilypond'
+ for f in '*'
+ ln -s /usr/share/bash-completion/lisp /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/lisp'
+ for f in '*'
+ ln -s /usr/share/bash-completion/mailman /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/mailman'
+ for f in '*'
+ ln -s /usr/share/bash-completion/mcrypt /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/mcrypt'
+ for f in '*'
+ ln -s /usr/share/bash-completion/modules /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/modules'
+ for f in '*'
+ ln -s /usr/share/bash-completion/mtx /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/mtx'
+ for f in '*'
+ ln -s /usr/share/bash-completion/p4 /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/p4'
+ for f in '*'
+ ln -s /usr/share/bash-completion/povray /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/povray'
+ for f in '*'
+ ln -s /usr/share/bash-completion/ri /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/ri'
+ for f in '*'
+ ln -s /usr/share/bash-completion/sbcl /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/sbcl'
+ for f in '*'
+ ln -s /usr/share/bash-completion/sitecopy /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/sitecopy'
+ for f in '*'
+ ln -s /usr/share/bash-completion/snownews /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/snownews'
+ for f in '*'
+ ln -s /usr/share/bash-completion/svk /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/svk'
+ for f in '*'
+ ln -s /usr/share/bash-completion/unace /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/unace'
+ for f in '*'
+ ln -s /usr/share/bash-completion/unrar /var/tmp/bash-completion-root/etc/bash_completion.d
+ echo '%ghost /etc/bash_completion.d/unrar'
+ cd -
/usr/src/redhat/BUILD/bash_completion
+ /usr/lib/rpm/redhat/brp-compress
+ /usr/lib/rpm/redhat/brp-strip /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-static-archive /usr/bin/strip
+ /usr/lib/rpm/redhat/brp-strip-comment-note /usr/bin/strip /usr/bin/objdump
+ /usr/lib/rpm/brp-python-bytecompile
+ /usr/lib/rpm/redhat/brp-java-repack-jars
Processing files: bash-completion-20060301-1
Executing(%doc): /bin/sh -e /var/tmp/rpm-tmp.28530
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd bash_completion
+ DOCDIR=/var/tmp/bash-completion-root/usr/share/doc/bash-completion-20060301
+ export DOCDIR
+ rm -rf /var/tmp/bash-completion-root/usr/share/doc/bash-completion-20060301
+ /bin/mkdir -p /var/tmp/bash-completion-root/usr/share/doc/bash-completion-20060301
+ cp -pr BUGS COPYING README TODO Changelog /var/tmp/bash-completion-root/usr/share/doc/bash-completion-20060301
+ exit 0
Provides: config(bash-completion) = 20060301-1
Requires(interp): /bin/sh /bin/sh
Requires(rpmlib): rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1
Requires(pre): /bin/sh
Requires: /bin/sh bash >= 2.05-12 config(bash-completion) = 20060301-1 fileutils grep sed textutils
Checking for unpackaged file(s): /usr/lib/rpm/check-files /var/tmp/bash-completion-root
Wrote: /usr/src/redhat/RPMS/noarch/bash-completion-20060301-1.noarch.rpm
Executing(%clean): /bin/sh -e /var/tmp/rpm-tmp.85514
+ umask 022
+ cd /usr/src/redhat/BUILD
+ cd bash_completion
+ rm -rf /var/tmp/bash-completion-root
+ exit 0
Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.85514
+ umask 022
+ cd /usr/src/redhat/BUILD
+ rm -rf bash_completion
+ exit 0
[root@rhel55 setup]#
使用rpmbuild编译后的rpm包在/usr/src/redhat/RPMS下,找到它,然后安装。
[root@rhel55 setup]# cd /usr/src/redhat/
BUILD/ RPMS/ SOURCES/ SPECS/ SRPMS/
[root@rhel55 setup]# cd /usr/src/redhat/ RPMS/
[root@rhel55 RPMS]# find . -name "bash-completion*rpm"
./noarch/bash-completion-20060301-1.noarch.rpm
[root@rhel55 RPMS]# rpm -ivh ./noarch/bash-completion-20060301-1.noarch.rpm
Preparing... ########################################### [100%]
1:bash-completion ########################################### [100%]
[root@rhel55 RPMS]# rpm -qa | grep bash
bash-3.2-24.el5
bash-completion-20060301-1
[root@rhel55 RPMS]# rpm -q bash-completion
bash-completion-20060301-1
[root@rhel55 RPMS]#
[root@rhel55 RPMS]#
安装成功了,退出重新登录。
[root@rhel55 RPMS]# exit
logout
下面的粗体的 TAB 表示按TAB键补全。
Last login: Thu Oct 28 13:19:28 2010 from 192.168.6.106
[root@rhel55 ~]# grep --TAB
--after-context= --count --fixed-strings --max-count= --silent
--basic-regexp --devices= --help --mmap --unix-byte-offsets
--before-context= --directories= --ignore-case --no-filename --version
--binary --exclude= --include= --no-messages --with-filename
--binary-files= --exclude-from= --invert-match --null --word-regexp
--byte-offset --extended-regexp --label= --null-data
--color --file= --line-buffered --only-matching
--colour --files-with-matches --line-number --perl-regexp
--context= --files-without-match --line-regexp --regexp=
[root@rhel55 ~]# cvs a TAB
add admin annotate
[root@rhel55 ~]# cvs a
示例二 在ssh命令后按tab键,bash会根据known_hosts中定义的主机自动补全
[root@web ~]# ssh 1TAB 92.168.6.1TAB
192.168.6.11 192.168.6.12
[root@web ~]# ssh 192.168.6.1
示例三 在service命令后按tab键,bash会自动根据/etc/init.d中的脚本名称自动补全
注意输入service命令之后输入空白再按TAB键,因为是用来补全参数的。如果输入service之后立即按TAB键,bash会认为是要补全命令。
[root@web ~]# service TAB
Display all 100 possibilities? (y or n)
acpid cvs hplip mdmonitor psacct sshd
anacron daytime-dgram ip6tables mdmpd rawdevices syslog
apmd daytime-stream ipmi messagebus rdisc tcpmux-server
atd discard-dgram iptables microcode_ctl readahead_early tftp
auditd discard-stream irda multipathd readahead_later time-dgram
autofs dnsmasq irqbalance netconsole restorecond time-stream
avahi-daemon dund isdn netfs rhnsd tomcat
avahi-dnsconfd echo-dgram jexec netplugd rmcp vncserver
bluetooth echo-stream kdump network rpcgssd wdaemon
capi eklogin killall NetworkManager rpcidmapd winbind
chargen-dgram ekrb5-telnet klogin nfs rpcsvcgssd wpa_supplicant
chargen-stream firstboot krb524 nfslock rsync xfs
conman gpm krb5-telnet nscd saslauthd xinetd
cpuspeed gssftp kshell ntpd sendmail ypbind
crond haldaemon kudzu pand setroubleshoot yum-updatesd
cups halt lvm2-monitor pcscd single
cups-config-daemon hidd mcstrans portmap smartd
[root@web ~]# service
示例四 在cvs命令后按tab键,会显示cvs自带的指令
[root@web ~]# cvs TAB
add ci diff freeze import patch rdiff rm tag --version
admin co -e get -l -q release rtag unedit -w
annotate commit edit -H log -Q remove stat up -x
-b -d export --help -n -r rfreeze status update -z
checkout delete -f history new rcs rlog -t -v
[root@web ~]# cvs
问题思考
1. 查看/etc/bash-completion文件,还会发现哪些命令的参数可以自动补全?
2. 怎样自行扩展一些命令参数的补全方式?