update-alternatives 命令

       update-alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.

       It is possible for several programs fulfilling the same or similar functions to be installed on a single system at the same time.  For example, many systems have several text editors installed at once.  This gives choice to the users of a system, allowing each to use a different editor,  if desired, but makes it difficult for a program to make a good choice for an editor to invoke if the user has not specified a particular preference.


whereis editor (当输入editor命令时,调用的是/usr/bin的edior命令,因为PATH路径中包含/usr/bin)

editor: /usr/bin/editor /usr/bin/X11/editor /usr/share/man/man1/editor.1.gz

ls  /usr/bin/editor -al

lrwxrwxrwx 1 root root 24 May 12  2013 /usr/bin/editor -> /etc/alternatives/editor

ls /usr/bin/editor -al

lrwxrwxrwx 1 root root 18 May 12  2013 /etc/alternatives/editor -> /usr/bin/vim.gnome(editor命令最终是调用/usr/bin/vim.gnome)


update-alternatives 用于配置 /etc/alternatives 对应命令的可选调用路径

例如: 

查看editor命令的候选选项

sudo update-alternatives --display editor

 editor - auto mode
  link currently points to /usr/bin/vim.gnome
  /bin/ed - priority -100
  slave editor.1.gz: /usr/share/man/man1/ed.1.gz
  /bin/nano - priority 40
  slave editor.1.gz: /usr/share/man/man1/nano.1.gz
 /usr/bin/vim.basic - priority 30
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
  slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
  slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
  slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz
  /usr/bin/vim.gnome - priority 60
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
  slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
  slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
  slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz
 /usr/bin/vim.tiny - priority 10
  slave editor.1.gz: /usr/share/man/man1/vim.1.gz
  slave editor.fr.1.gz: /usr/share/man/fr/man1/vim.1.gz
  slave editor.it.1.gz: /usr/share/man/it/man1/vim.1.gz
  slave editor.pl.1.gz: /usr/share/man/pl/man1/vim.1.gz
  slave editor.ru.1.gz: /usr/share/man/ru/man1/vim.1.gz
 Current 'best' version is '/usr/bin/vim.gnome'.

 (editor 命令的候选选项有6个,当前链接为/usr/bin/vim.gnome


增加命令的候选选项

sudo update-alternatives --install /usr/bin/editor editor /usr/bin/gedit 300

 /usr/bin/editor : generic name (or alternative link)
                             A name, like /usr/bin/editor, which refers, via the alternatives system, to one of a number of files of similar function.

editor:  alternative name. The name of a symbolic link in the alternatives directory.

 /usr/bin/gedit : path

300:  priority(默认选中优先级最高的)


更改命令的链接选项

sudo update-alternatives --config editor

  

There are 5 choices for the alternative editor (providing /usr/bin/editor).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/vim.gnome   60        auto mode
  1            /bin/ed             -100       manual mode
  2            /bin/nano            40        manual mode
  3            /usr/bin/vim.basic   30        manual mode
  4            /usr/bin/vim.gnome   60        manual mode
  5            /usr/bin/vim.tiny    10        manual mode

Press enter to keep the current choice[*], or type selection number:(输入你要选定的选项)




 

在使用 `sudo update-alternatives --config` 切换版本之前,必须先通过 `sudo update-alternatives --install` 注册不同版本的编译器及其优先级。这是因为在 `/etc/alternatives` 中并没有预先配置这些编译器的不同版本,因此系统无法知道有哪些可选版本可供切换。 具体步骤如下: 1. **注册编译器**:使用 `sudo update-alternatives --install` 命令将不同版本的编译器(如 `gcc`, `g++`, `gfortran`)及其路径和优先级注册到系统的替代品管理中。 ```bash sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 40 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 50 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 40 sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 50 sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-11 40 sudo update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-9 50 ``` 2. **选择编译器**:使用 `sudo update-alternatives --config` 命令从已注册的版本中选择当前使用的编译器版本。 ```bash sudo update-alternatives --config gcc sudo update-alternatives --config g++ sudo update-alternatives --config gfortran ``` 如果不执行 `sudo update-alternatives --install` 步骤,系统将不知道存在哪些版本的编译器,从而无法通过 `sudo update-alternatives --config` 进行切换。因此,这两个命令是相辅相成的,缺一不可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值