2019独角兽企业重金招聘Python工程师标准>>>
这个命令相当于windows下的选择文件的打开方式(或者叫关联文件类型),比如要编辑一个文件,可以用notepad也可以用notepad++, editplus等等,那么ubuntu下是用update-alternatives来管理的,他的原理是在/etc/alternatives目录下为每个管理的程序建立一个软连接,man他可以找到为什么要这么设计。
时间宝贵,就不废话了,转几篇文章结合着阅读:
文章一
如果安装了同一个软件的不同版本,可以使用update-alternatives命令设置默认使用哪个版本,典型的如在Ubuntu 12.04里安装了gcc-4.6和gcc-4.8,想让gcc命令自动使用4.8版。
安装g++-4.8后,将其设置为默认。gcc同理
# 首先要让系统知道我们安装了多个版本的g++
# 命令最后的 20和50是优先级,如果使用auto选择模式,系统将默认使用优先级高的
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
# 使用交互方式的命令选择默认使用的版本
sudo update-alternatives --config g++
# 其他命令:
# 查询系统中安装有哪些版本
sudo update-alternatives --query g++
文章二: 安装jdk
Move the downloaded jdk-7u11-linux-i586.tar.gz archive binary to the directory you want to install.
Open a Terminal, the the command to unpack the tarball and install the JDK:
$tar zxvf jdk-7u11-linux-i586.tar.gz
For example, if you download the .tar.gz is /home/you, your installed directory will be /home/you/jdk1.7.0_11.
Then, you have to update alternatives for javac and java:
$sudo update-alternatives --install /usr/bin/javac javac /home/you/jdk1.7.0_11/bin/javac 1
$sudo update-alternatives --install /usr/bin/java java /home/you/jdk1.7.0_11/bin/java 1
If you have more than one javac installed, you have to config your javac alternative
$sudo update-alternatives --config javac
$sudo update-alternatives --config java
Finally, delete the downloaded .tar.gz after then.
转自:http://www.tuicool.com/articles/fERbQn
Cyper实战:
尽管我装了jdk,设置了环境变量,也用了很久了, 但是既然学习了alternatives, 实践一把,过程如下:
cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ update-alternatives --config java #没配置过的情况
update-alternatives: error: no alternatives for java.
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/java java `which java` 1 #在此处进行首次配置
[sudo] password for cyper:
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/java to provide /usr/bin/java (java) in auto mode.
cyper@zerorun:~$ update-alternatives --config java #只一个jdk版本的情况
There is only one alternative in link group java: /opt/jdk/jdk1.7.0_45/bin/java
Nothing to configure.
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/javac javac `which javac` 1 #同样安装javac
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/javac to provide /usr/bin/javac (javac) in auto mode.
cyper@zerorun:~$ ls /usr/bin/java* #新建的link
/usr/bin/java /usr/bin/javac
cyper@zerorun:~$ ls -l /usr/bin/java*
lrwxrwxrwx 1 root root 22 Dec 20 23:07 /usr/bin/java -> /etc/alternatives/java
lrwxrwxrwx 1 root root 23 Dec 20 23:07 /usr/bin/javac -> /etc/alternatives/javac
cyper@zerorun:~$ ls -l /etc/alternatives/java* #在etc下也有新建link
lrwxrwxrwx 1 root root 29 Dec 20 23:07 /etc/alternatives/java -> /opt/jdk/jdk1.7.0_45/bin/java
lrwxrwxrwx 1 root root 30 Dec 20 23:07 /etc/alternatives/javac -> /opt/jdk/jdk1.7.0_45/bin/javac
cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ sudo update-alternatives --remove java `which java` #删除某个path中的java
cyper@zerorun:~$ ls -l /usr/bin/java #如果唯一的path被删除, link也会被删除
ls: cannot access /usr/bin/java: No such file or directory
cyper@zerorun:~$ sudo update-alternatives --remove-all java #删除全部的java配置
update-alternatives: error: no alternatives for java.
cyper@zerorun:~$ which java
/opt/jdk/jdk1.7.0_45/bin/java
cyper@zerorun:~$ sudo update-alternatives --set java `which java` #这个是设置文件的关联类型
update-alternatives: error: no alternatives for java. #找不到任何path可以关联
cyper@zerorun:~$ sudo update-alternatives --install /usr/bin/java java `which java` 1 #重新安装
update-alternatives: using /opt/jdk/jdk1.7.0_45/bin/java to provide /usr/bin/java (java) in auto mode.
cyper@zerorun:~$ sudo update-alternatives --set java `which java` #再次尝试设置文件的关联类型,虽然没有意义,但这回没有错误
注意:install的格式如下:
sudo update-alternatives --install link name path priority,很好理解
命令行中的`which java`就是 /opt/jdk/jdk1.7.0_45/bin/java,懒得写全路径。
可以看出针对java命令, 他做了如下事情:
1)新建了一个符号链接/usr/bin/java, 指向/etc/alternatives/java
2)而这第二个文件同样也是一个符号链接,指向了/opt/jdk/****,你实际jdk的安装目录。
因为我只装了一个版本的jdk,所以使用sudo update-alternatives --config java暂时没什么用
最后玩玩--query , --set
cyper@zerorun:~$ sudo update-alternatives --query java #查询一个path的情况
Link: java
Status: manual
Best: /opt/jdk/jdk1.7.0_45/bin/java
Value: /opt/jdk/jdk1.7.0_45/bin/java
Alternative: /opt/jdk/jdk1.7.0_45/bin/java
Priority: 1
cyper@zerorun:~$ sudo update-alternatives --query editor #查询多个path的情况
Link: editor
Status: auto
Best: /bin/nano
Value: /bin/nano
Alternative: /bin/ed
Priority: -100
Slaves:
editor.1.gz /usr/share/man/man1/ed.1.gz
A cyper@zerorun:~$ sudo update-alternatives --config editor #一般都会这么设置文件的关联类型
There are 4 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
* 0 /bin/nano 40 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.tiny 10 manual mode
Press enter to keep the current choice[*], or type selection number: ^Ccyper@zerorun:~$ ^C
cyper@zerorun:~$ sudo update-alternatives --set editor 2 #异想天开
update-alternatives: error: alternative 2 for editor not registered, not setting.
cyper@zerorun:~$ sudo update-alternatives --set editor /usr/bin/vim.basic #极客都这么设置(无需交互)
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in manual mode.
cyper@zerorun:~$ sudo update-alternatives --config editor #设置成功
There are 4 choices for the alternative editor (providing /usr/bin/editor).
Selection Path Priority Status
------------------------------------------------------------
0 /bin/nano 40 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.tiny 10 manual mode
有不清楚的地方man update-alternatives