linux 软件多版本切换:alternatives

1, 功能描述: 维护符号链接以确定默认命令

  • 可以将具有相同或相似功能的多个程序同时安装在单个系统上。例如,许多系统同时安装了多个文本编辑器。这为系统的用户提供了选择,允许每个用户使用不同的编辑器(如果需要),但是如果用户未指定特定的首选项,则程序很难做出一个好的选择来调用编辑器。

  • 每一个 alternative 维护的名称都对应一个priority优先级, 在automatic 模式下,该名称会自动指向优先级高的

#软件包
[root@test-c6 ~]# which alternatives
/usr/sbin/alternatives
[root@test-c6 ~]# rpm -qf /usr/sbin/alternatives
chkconfig-1.3.49.5-1.el6.x86_64

[root@test-c6 ~]# rpm -ql chkconfig |grep alternative
/etc/alternatives
/usr/sbin/alternatives
/usr/sbin/update-alternatives
/usr/share/man/man8/alternatives.8.gz
/usr/share/man/man8/update-alternatives.8.gz
/var/lib/alternatives


#使用方法
[root@test-c6 ~]# alternatives
usage: alternatives --install <link> <name> <path> <priority>
                    [--initscript <service>]
                    [--slave <link> <name> <path>]*
       alternatives --remove <name> <path>
       alternatives --auto <name>
       alternatives --config <name>
       alternatives --display <name>
       alternatives --set <name> <path>

common options: --verbose --test --help --usage --version
                --altdir <directory> --admindir <directory>

基本使用

案例: 自定义一个脚本,放在/usr/local/sbin里面,使得PATH能找到;现在要维护其版本:比如1.0, 2.0这种

## 版本1.0
[root@test-c6 ~]# cat /tmp/test.sh
echo 123; date

[root@test-c6 ~]# cat /tmp/test2.sh
## 版本2.0
echo 456; date

#第一步: 使用alternatives管理脚本
# 多版本维护/切换: 名称为test.sh
#				  alternatives --install  <dest link> 		   <别名name>  <src path> 	<priority>
[root@test-c6 ~]# alternatives --install  /usr/local/sbin/test.sh test.sh /tmp/test.sh 100
## 查看信息
[root@test-c6 ~]# alternatives --display test.sh
test.sh - status is auto.
 link currently points to /tmp/test.sh
/tmp/test.sh - priority 100
Current `best' version is /tmp/test.sh.

#第二步: 指定test.sh 使用版本2.0
[root@test-c6 ~]# alternatives --install  /usr/local/sbin/test.sh test.sh /tmp/test2.sh 200
## 查看信息
[root@test-c6 ~]# alternatives --display test.sh
test.sh - status is auto.
 link currently points to /tmp/test2.sh
/tmp/test.sh - priority 100
/tmp/test2.sh - priority 200
Current `best' version is /tmp/test2.sh.

#第三步:切换手动模式/自动模式
# 切换为自动模式:   			   alternatives --auto test.sh

# 切换手动默认:不自动指向最高优先级的版本
# 非交互式切手动模式和指向的优先级: alternatives --set test.sh /tmp/test2.sh 
[root@test-c6 ~]# alternatives --config test.sh
There are 2 programs which provide 'test.sh'.
  Selection    Command
-----------------------------------------------
   1           /tmp/test.sh
*+ 2           /tmp/test2.sh

Enter to keep the current selection[+], or type selection number: 1
[root@test-c6 ~]# alternatives --display test.sh
test.sh - status is manual.
 link currently points to /tmp/test.sh
/tmp/test.sh - priority 100
/tmp/test2.sh - priority 200
Current `best' version is /tmp/test2.sh.
  • 查看效果
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/eyeofeagle/article/details/111836681
今日推荐