http://blog.sina.com.cn/s/blog_3fc8570201000cny.html

对于一些程序, Debian提供了这些程序的多种不同实现. 如awk提供了gawk,nawk, java提供了gij, sun-java5, sun-java6等. 同时Debian也提供了update-alternatives来切换多个不同的实现. 



不使用任何参数运行update-alternatives命令, 则显示命令的帮助



blueice:~# update-alternatives
update-alternatives: need --display, --config, --set, --install, --remove, --all, --remove-all or --auto

Usage: update-alternatives [<option> ...] <command>

Commands:
  --install <link> <name> <path> <priority>
    [--slave <link> <name> <path>] ...
                           add a group of alternatives to the system.
  --remove <name> <path>   remove <path> from the <name> group alternative.
  --remove-all <name>      remove <name> group from the alternatives system.
  --auto <name>            switch the master link <name> to automatic mode.
  --display <name>         display information about the <name> group.
  --list <name>            display all targets of the <name> group.
  --config <name>          show alternatives for the <name> group and ask the
                           user to select which one to use.
  --set <name> <path>      set <path> as alternative for <name>.
  --all                    call --config on all alternatives.

<link> is the symlink pointing to /etc/alternatives/<name>.
  (e.g. /usr/bin/pager)
<name> is the master name for this link group.
  (e.g. pager)
<path> is the location of one of the alternative target files.
  (e.g. /usr/bin/less)
<priority> is an integer; options with higher numbers have higher priority in
  automatic mode.

Options:
  --altdir <directory>     change the alternatives directory.
  --admindir <directory>   change the administrative directory.
  --test                   don't do anything, just demonstrate.
  --verbose                verbose operation, more output.
  --quiet                  quiet operation, minimal output.
  --help                   show this help message.
  --version                show the version.

update-alternatives --list <command> 列出<command>可用的候选命令, 例如



blueice:~# update-alternatives --list java
/usr/bin/gij-wrapper-4.0
/usr/bin/gij-wrapper-4.1
/usr/lib/jvm/java-gcj/jre/bin/java
/usr/lib/jvm/java-6-sun/jre/bin/java

update-alternatives --config <command>可用来显示和修改<command>实际指向的候选命令, 例如



blueice:~# update-alternatives --config java

There are 4 alternatives which provide `java'.

  Selection    Alternative
-----------------------------------------------
          1    /usr/bin/gij-wrapper-4.0
          2    /usr/bin/gij-wrapper-4.1
 +        3    /usr/lib/jvm/java-gcj/jre/bin/java
*         4    /usr/lib/jvm/java-6-sun/jre/bin/java

Press enter to keep the default[*], or type selection number: 4
Using `/usr/lib/jvm/java-6-sun/jre/bin/java' to provide `java'.

其中, '+'表示推荐的候选命令, '*'表示实际指向的候选命令.



update-alternatives --all则可以配置系统上所有的候选命令.



事实上, update-alternatives通过修改/etc/alternatives下的链接来实现候选命令的更改.



blueice:~# ls -al /usr/bin/java


lrwxrwxrwx 1 root root 22 Aug 30  2005 /usr/bin/java -> /etc/alternatives/java


blueice:~# ls -al /etc/alternatives/java


lrwxrwxrwx 1 root root 36 Jul 24 22:21 /etc/alternatives/java -> /usr/lib/jvm/java-6-sun/jre/bin/java


blueice:~# s -al /usr/lib/jvm/java-6-sun/jre/bin/java


-rwxr-xr-x 1 root root 47116 Jun 15 07:26 /usr/lib/jvm/java-6-sun/jre/bin/java