2022年8月4日 星期四

update-alternative to switch version of the command

env:
Debian update-alternatives version 1.19.7.
#!/bin/bash

function Install {
    SBUF=""

    if [ $DEBUG -eq 1 ]; then
        echo "update-alternatives"
    else
        SBUF="update-alternatives"
    fi

    # find main bin file to install
    while IFS= read -r bin_file
    do
        if [ "$bin_file" == "$1" ]; then
            if [ $DEBUG -eq 1 ]; then
                echo " --install $PATH_F/$1-$VERSION  $bin_file  $PATH_F/$1  $PRIORITY"
            else
                SBUF=$SBUF" --install $PATH_F/$1-$VERSION  $bin_file  $PATH_F/$1  $PRIORITY"
            fi
        fi
    done < <(ls $PATH_F)
    
    # find other bin file to slave
    while IFS= read -r bin_file
    do
        if [ "$bin_file" == "$1" ]; then
            continue;
        else
            if [ $DEBUG -eq 1 ]; then
                echo " --slave $PATH_F/$bin_file-$VERSION  $bin_file  $PATH_F/$bin_file"
            else
                SBUF=$SBUF" --slave $PATH_F/$bin_file-$VERSION  $bin_file  $PATH_F/$bin_file"
                #SBUF=$SBUF" bbb"
            fi
        fi
    done < <(ls $PATH_F)
    echo SBUF=$SBUF
    ${SBUF};

    #print the result
    if [ $? -eq 0 ] && [ $DEBUG -eq 0 ];then
        echo "Alreay install the $1 setting with update-alternatives"
        exit 0
    elif [ $DEBUG -eq 1 ]; then
        exit 0
    else
        echo "Error, please check again with DEBUG mode"
        exit 1
    fi

}

function Remove {
    SBUF=""

    if [ $DEBUG -eq 1 ]; then
        echo "update-alternatives --remove $1 $PATH_F/$1"
    else
        SBUF="update-alternatives --remove $1 $PATH_F/$1"
    fi

    #print the result
    if [ $? -eq 0 ] && [ $DEBUG -eq 0 ];then
        echo "Alreay remove the $1 setting with update-alternatives"
        exit 0
    elif [ $DEBUG -eq 1 ]; then
        exit 0
    else
        echo "Error, please check again with DEBUG mode"
        exit 1
    fi
}

PATH_F="/usr/lib/jvm/jdk1.7.0_25/bin"
VERSION=1.7.0.25
PRIORITY=1075

DEBUG=0 #only print the result
REMOVE=1
if [ $REMOVE -eq 1 ]; then
    Remove java
else
    Install java
fi

list
$ update-alternatives --get-selections

Display status
$ update-alternatives --display java
$ update-alternatives --query java

Switch version
$ update-alternatives --config java
ref:
1.update-alternatives︰管理指令和檔案的多個版本
2.update-alternatives-clang.sh
3.update-alternatives for clang

沒有留言:

張貼留言