linux sh : get os info, gcc version, glibc version
发布日期:2021-06-30 22:17:05
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月25日 19时10分07秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Android 开发学习笔记 00 - Getting Started
2019-04-30
【学习笔记】Android Activity
2019-04-30
Android使用Retrofit_00_Getting Started
2019-04-30
Android使用Retrofit_01_OAuth2 + GitHub
2019-04-30
OpenCV杂记 - Mat in C++
2019-04-30
location区段
2019-04-30
nginx访问控制、基于用户认证、https配置
2019-04-30
SaltStack
2019-04-30
linux内存的寻址方式
2019-04-30
how2heap-double free
2019-04-30
how2heap-fastbin_dup_consolidate
2019-04-30
orw_shellcode_模板
2019-04-30
fmt在bss段(neepusec_easy_format)
2019-04-30
python 函数式编程
2019-04-30
tensorflow 数据格式
2019-04-30
tf keras SimpleRNN源码解析
2019-04-30
tf keras Dense源码解析
2019-04-30
检验是否服从同一分布
2019-04-30