linux sh : get os info, gcc version, glibc version
发布日期:2021-06-30 22:17:05 浏览次数:2 分类:技术文章

本文共 4610 字,大约阅读时间需要 15 分钟。

前言

学了一个星期脚本编程,该写安装程序了。

还有点前置任务要做。

安装时,假设编译了几个不同版本的目标程序集合,e.g. 32bits, 64bits; redhat or debian; gcc have or not; glibc version depend.

安装时,要判断依赖条件是否满足,安装包内是否有适合的目标程序集合可以安装。

这就要先得到当前linux系统的信息,gcc版本,glibc版本。
捣鼓了一天,封装了几个函数,将取信息和比较信息的测试代码写好了

运行效果

>> func_test, test case for ./media/helper/sh.os_infothis OS is linuxfunc_is_64bits_os() = [64bits]func_get_os_name() = [Debian]func_get_os_ver() = [7.5]func_get_gcc_ver() = [4.7.2]func_get_glibc_ver() = [2.13]install now, please wait ...install over, thank you :)

实验

#!/bin/bash# @file ./media/test_case/test_case.sh# @brief test case for ./media/helper/sh.os_info. ../helper/sh.os_infoMIN_GCC_VER="3.0"MIN_LIBC_VER="2.09"function func_test() {    local rc=0    local str_tmp=""    local str_gcc_ver=""    local str_glibc_ver=""    clear    echo -e     echo -e ">> func_test, test case for ./media/helper/sh.os_info"    echo -e    func_is_linux_os    rc=$?    if [ $rc -eq 0 ]    then        str_tmp="this OS is linux"        echo "$str_tmp"    else        str_tmp="install error : this OS isn't linux, installer can't be run"        echo "$str_tmp"        return 255    fi    func_is_64bits_os    rc=$?    if [ $rc -eq 0 ]    then        str_tmp="64bits"        echo -e "func_is_64bits_os() = [$str_tmp]"    else        str_tmp="not 64bits OS"        echo -e "func_is_64bits_os() = [$str_tmp]"        str_tmp="install error : this OS isn't 64bits linux, installer can't be run"        echo "$str_tmp"        return 254    fi    echo -e "func_get_os_name() = [$(func_get_os_name)]"    echo -e "func_get_os_ver() = [$(func_get_os_ver)]"    # maybe need gcc to build some prog    str_gcc_ver=$(func_get_gcc_ver)    echo -e "func_get_gcc_ver() = [$str_gcc_ver]"    if [[ "$str_gcc_ver" < "$MIN_GCC_VER" ]]    then        str_tmp="install error : gcc version need >= $MIN_GCC_VER"        echo "$str_tmp"        return 253    fi    str_glibc_ver=$(func_get_glibc_ver)    echo -e "func_get_glibc_ver() = [$str_glibc_ver]"    if [[ "$str_glibc_ver" < "$MIN_LIBC_VER" ]]    then        str_tmp="install error : glibc version need >= $MIN_LIBC_VER"        echo "$str_tmp"        return 252    fi    echo "install now, please wait ..."    sleep 6    echo "install over, thank you :)"}# test casefunc_testexit 0
#!/bin/bash# @file ./media/helper/sh.os_info# @brief get OS info# @fn func_remove_space# @brief remove all spaces from IN param1 stringfunction func_remove_space() {    local rv=$(echo -e $1 | sed "s/ //")    echo "$rv"    return 0}function func_get_os_name() {    # method 1, use system command and awk filter    # org sring below, one row    # Distributor ID:   Debian    local rv=$(lsb_release -i)    # get part 2 as obj, split char is ':'    rv=$(echo -e "$rv" | awk -F: ' { print $2 } ')    # remove space, from "  Debian" to "Debian"    echo "$(func_remove_space $rv)"    return 0}function func_get_os_ver() {    # method 2, only use system command    # below code only display "7.5" :)    echo "$(lsb_release -s -r)"    return 0}function func_is_linux_os() {    # uname -s # kernel name    # Linux    # rc = 0, is    # rc = other, is unknow    local rc=-1    local rv=$(uname -s)    if [ "$rv" == "Linux" ]    then        rc=0    fi    return $rc}function func_is_64bits_os() {    # # uname -m    # x86_64    # rc = 0, is 64bits    # rc = other, is unknow    local rc=-1    local rv=$(uname -m)    if [ "$rv" == "x86_64" ]    then        rc=0    fi    return $rc}function func_get_gcc_ver() {    local rv=$(gcc --version)    # get first line below    # gcc (Debian 4.7.2-5) 4.7.2    rv=$(echo -e "$rv" | sed -n '1p')    # get last word below    # 4.7.2    rv=$(echo -e "$rv" | awk -F' ' '{ print $NF }')    echo "$rv"    return 0}function func_get_glibc_ver() {    # get this .sh use what glibc path name    local rv=$(lsof -p $$ | grep libc-)    # test_case 59995 root  mem    REG    8,1  1599504 1835011 /lib/x86_64-linux-gnu/libc-2.13.so    # get last world by split ' '    rv=$(echo -e "$rv" | awk -F' ' '{ print $NF }')    # /lib/x86_64-linux-gnu/libc-2.13.so    # make command string : "libc-x.so --version"    rv=$(echo "$rv --version")    # execute libc-x.so --version    rv=$(eval "$rv")    # get first row below    # GNU C Library (Debian EGLIBC 2.13-38+deb7u1) stable release version 2.13, by Roland McGrath et al.    rv=$(echo -e "$rv" | sed -n '1p')    # get last world by split "release version "    rv=$(echo -e "$rv" | awk -F"release version " '{ print $2 }')    # 2.13, by Roland McGrath et al.    # get first world by split ","    rv=$(echo -e "$rv" | awk -F"," '{ print $1 }')    # yes, get glibc version "2.13"    echo "$rv"    return 0}

转载地址:https://lostspeed.blog.csdn.net/article/details/79335297 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:linux sh : check sh function is exist; multiple line comment;
下一篇:linux sh : bashdb的安装和初步使用

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月28日 14时28分42秒