zoukankan      html  css  js  c++  java
  • alternatives 命令学习

    最经在捣鼓Cloudera的cdh ,发现里面使用了alternatives命令,由于不懂这个命令,让我走了好多弯路。

    现在mark一下

    ubuntu 12.04 系统的命令为:update-alternatives

    RedHat 6.4 系统命令为:alternatives,其中update-alternatives命令是/usr/sbin/alternatives的软链接。

    现在以ubuntu 12.04 系统为基础。

    假设系统中一个程序,存在多个版本

    /opt/chenfool/chent1/chentest

    代码

    #!/bin/bash
    
    echo "test1"

    /opt/chenfool/chent2/chentest

    代码

    #!/bin/bash
    
    echo "test2"

    将/opt/chenfool/chent1/chentest 程序加入alternatives管理中

    update-alternatives --install /usr/sbin/chentest chentest /opt/chenfool/chent1/chentest 1

    将/opt/chenfool/chent1/chentest 程序加入alternatives管理中

    update-alternatives --install /usr/sbin/chentest chentest /opt/chenfool/chent2/chentest 2

    说明:

    update-alternatives --install <link> <name> <path> <priority>

    link:是目标的程序

    name:是指定你程序中在alternatives中的名字(建议与link同名),它会在/etc/alternatives目录下建立同名文件,同时,它也是个软链接

    path:是你要真实的程序路径

    priority:是你程序的版本号

    建立好一个映射关系后,可以通过命令查看你的程序版本

    update-alternatives --display chentest

    输出:

    /opt/chenfool/chent1/chentest - priority 1
    /opt/chenfool/chent2/chentest - priority 2

    选择你需要的版本

    update-alternatives --config chentest

    输出:

      Selection    Path                           Priority   Status
    ------------------------------------------------------------
    * 0            /opt/chenfool/chent2/chentest   2         auto mode
      1            /opt/chenfool/chent1/chentest   1         manual mode
      2            /opt/chenfool/chent2/chentest   2         manual mode

    Press enter to keep the current choice[*], or type selection number:

    输入你的选择的版本即可。

    原理:

    /usr/sbin/chentest—>/etc/alternatives/chentest—>/opt/chenfool/chent1/chentest

    它实质就是通过一个软链接来实现不同版本的选择。

    alternatives存储对应关系的文件

    ubuntu系统:/var/lib/dpkg/alternatives/chentest

    RedHat系统:/var/lib/alternatives/chentest

    manual
    /usr/sbin/chentest
    
    /opt/chenfool/chent1/chentest
    1
    /opt/chenfool/chent2/chentest
    2

    删除一个版本

    update-alternatives --remove chentest /opt/chenfool/chent2/chentest

    从alternatives 管理中删除一个程序,/usr/sbin/chentest会被删除

    update-alternatives --remove-all chentest

    手动清理alternatives的文件

    rm -f /var/lib/dpkg/alternatives/chentest
    rm -f /etc/alternatives/chentest
    rm -f /usr/sbin/chentest

    参考文章:

    http://blog.csdn.net/chszs/article/details/4158485

  • 相关阅读:
    Python脚本运行出现语法错误:IndentationError: unindent does not match any outer indentation level
    Python3 运算符
    Python3 注释
    Python3 解释器
    Python3 环境搭建
    Python 3 教程
    Python3 基本数据类型
    趣闻|Python之禅(The Zen of Python)
    ios开发笔记根据传入字符串的长度动态生成label,并按照屏幕宽度排列
    iOS开发设置tableview的分割线
  • 原文地址:https://www.cnblogs.com/chenfool/p/3759037.html
Copyright © 2011-2022 走看看