博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux sh : get os info, gcc version, glibc version
阅读量:1993 次
发布时间:2019-04-27

本文共 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}

转载地址:http://xymvf.baihongyu.com/

你可能感兴趣的文章
腾讯AI语音识别API踩坑记录
查看>>
linux中Oops信息的调试及栈回溯—Linux人都知道,这是好东西!
查看>>
C语言与javascript的不同之处
查看>>
Android照相功能驱动层中HAL的实现(基于OK6410开发板+OV9650摄像头)
查看>>
Android Camera HAL设计初步
查看>>
基于U-boot上TFTP服务器更新系统
查看>>
P4139 上帝与集合的正确用法
查看>>
P3811 【模板】乘法逆元
查看>>
P5431 【模板】乘法逆元2
查看>>
P1495 【模板】中国剩余定理(CRT)/曹冲养猪
查看>>
P3868 [TJOI2009]猜数字
查看>>
YbtOJ——贪心算法【例题3】畜栏预定
查看>>
YbtOJ——贪心算法【例题2】雷达装置
查看>>
YbtOJ——递推算法【例题4】传球游戏
查看>>
YbtOJ——深度搜索【例题1】拔河比赛
查看>>
YbtOJ——深度搜索【例题2】数独游戏
查看>>
YbtOJ——字符串处理【例题1】数字反转
查看>>
YbtOJ——字符串处理【例题2】移位包含
查看>>
YbtOJ——广度搜索【例题2】山峰和山谷
查看>>
YbtOJ——广度搜索【例题3】立体推箱子
查看>>