天天看點

bash_completionq強勁的bash參數補全功能

本文連結: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: 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

+ 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 .

+ /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

+ 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'

+ ln -s /usr/share/bash-completion/bittorrent /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/bittorrent'

+ ln -s /usr/share/bash-completion/cksfv /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/cksfv'

+ ln -s /usr/share/bash-completion/clisp /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/clisp'

+ ln -s /usr/share/bash-completion/dsniff /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/dsniff'

+ ln -s /usr/share/bash-completion/freeciv /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/freeciv'

+ ln -s /usr/share/bash-completion/gcl /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/gcl'

+ ln -s /usr/share/bash-completion/gkrellm /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/gkrellm'

+ ln -s /usr/share/bash-completion/gnatmake /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/gnatmake'

+ ln -s /usr/share/bash-completion/harbour /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/harbour'

+ ln -s /usr/share/bash-completion/hg /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/hg'

+ ln -s /usr/share/bash-completion/isql /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/isql'

+ ln -s /usr/share/bash-completion/larch /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/larch'

+ ln -s /usr/share/bash-completion/lilypond /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/lilypond'

+ ln -s /usr/share/bash-completion/lisp /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/lisp'

+ ln -s /usr/share/bash-completion/mailman /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/mailman'

+ ln -s /usr/share/bash-completion/mcrypt /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/mcrypt'

+ ln -s /usr/share/bash-completion/modules /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/modules'

+ ln -s /usr/share/bash-completion/mtx /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/mtx'

+ ln -s /usr/share/bash-completion/p4 /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/p4'

+ ln -s /usr/share/bash-completion/povray /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/povray'

+ ln -s /usr/share/bash-completion/ri /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/ri'

+ ln -s /usr/share/bash-completion/sbcl /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/sbcl'

+ ln -s /usr/share/bash-completion/sitecopy /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/sitecopy'

+ ln -s /usr/share/bash-completion/snownews /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/snownews'

+ ln -s /usr/share/bash-completion/svk /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/svk'

+ ln -s /usr/share/bash-completion/unace /var/tmp/bash-completion-root/etc/bash_completion.d

+ echo '%ghost /etc/bash_completion.d/unace'

+ 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

+ 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

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

+ rm -rf /var/tmp/bash-completion-root

Executing(--clean): /bin/sh -e /var/tmp/rpm-tmp.85514

[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 

[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. 怎樣自行擴充一些指令參數的補全方式?

繼續閱讀