linux sh : LFS env check
发布日期:2021-06-30 22:17:26 浏览次数:2 分类:技术文章

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

前言

LFS原版的version-check.sh,是一直运行完的,如果有错误,要自己看,不友好。

改了一下,如果检测到环境不符合,就提示错误并结束.
这样,哪个环境缺了,就补哪个, 交互性好很多.
更好的交互方式是将出错信息和正常的信息用颜色分开,一次性检测完, 如果有错误,一目了然. 以后可以优化下。

LFS对组件的版本好像有要求, 这里可以先不加入具体版本的检查,如果后面编不过,再换系统组件版本也不迟,反正都是搞脚本自动来编译的。其实是否会因为系统组件的版本不同而编译不过,这都不好说,只是LFS的作者只在他自己指定的组件版本清单中编译过了,那些版本号,是作者推荐的。作者是怕耽误做实验同学的时间。走弯路,我倒是不怕,反正做实验,就是一通折腾,第一次玩,走弯路不可避免的。

现在流行的发行版,只要有这些组件,作为宿主系统,编译LFS,应该都能编译过吧。

运行效果

--------------------------------------------------------------------------------[LFS env check][1.0.1.3 alpha][2018-04-06 22:59]--------------------------------------------------------------------------------[1] check bash ...    [ok] GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)[2] check /bin/sh ...    [ok] /bin/bash[3] check Binutils ...    [ok] GNU ld version 2.20.51.0.2-5.36.el6 20100205[4] check bison ...    [ok] bison (GNU Bison) 2.4.1[5] check yacc ...    [ok] /usr/bin/yacc - 1.9 20070509[6] check bzip2 ...    [ok] bzip2, a block-sorting file compressor.  Version 1.0.5, 10-Dec-2007.[7] check Coreutils ...    [ok] chown (GNU coreutils) 8.4[8] check diff ...    [ok] diff (GNU diffutils) 2.8.1[9] check find ...    [ok] find (GNU findutils) 4.4.2[10] check gawk ...    [ok] GNU Awk 3.1.7[11] check awk ...    [ok] /bin/gawk[12] check gcc ...    [ok] gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)[13] check g++ ...    [ok] g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-18)[14] check ldd ...    [ok] ldd (GNU libc) 2.12[15] check grep ...    [ok] GNU grep 2.6.3[16] check gzip ...    [ok] gzip 1.3.12[17] check Linux Kernel ...    [ok] Linux version 2.6.32-431.el6.x86_64 (mockbuild@c6b8.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) ) #1 SMP Fri Nov 22 03:15:09 UTC 2013[18] check m4 ...    [ok] m4 (GNU M4) 1.4.13[19] check make ...    [ok] GNU Make 3.81[20] check patch ...    [ok] patch 2.6[21] check perl ...    [ok]. version='5.10.1';[22] check sed ...    [ok] GNU sed version 4.2.1[23] check tar ...    [ok] tar (GNU tar) 1.23[24] check makeinfo ...    [ok] makeinfo (GNU texinfo) 4.13[25] check xz ...    [ok] xz (XZ Utils) 4.999.9beta[26] check g++ compilation    [ok] g++ compilation successcheck lib depend ...[27] check lib [libmpfr.la]...    ok : find [libmpfr.la] = [/usr/lib/libmpfr.la][28] check lib [libmpc.la]...    ok : find [libmpc.la] = [/usr/lib/libmpc.la][29] check lib [libgmp.la]...    ok : find [libgmp.la] = [/usr/lib/libgmp.la]env check success

脚本

#!/bin/bash# @file version-check.sh# Simple script to list version numbers of critical development toolsdeclare -r PROG_NAME="LFS env check"declare -r PROG_VER="1.0.1.3 alpha"declare -r PROG_TIME="2018-04-06 22:59"declare -r LINE80="--------------------------------------------------------------------------------"declare -r RC_OK=0declare -r RC_ERR=1declare -r ERR_CMD_NOT_FOUND="command not found"export LC_ALL=Cfunction show_version() {    echo -e "$LINE80"    printf "[%s][%s][%s]\n" "$PROG_NAME" "$PROG_VER" "$PROG_TIME"    echo -e "$LINE80"}function clear_screen() {    declare var i=25    clear    # -gt means >    # -ge means >=    # -eq means ==    # -ne means !=    # -lt means <    # -le means <    while [ $i -gt 0 ]    do        echo -e        i=$(( $i - 1 ))    done}function find_sub_string() {    # $1 is src    # $2 is the sub string to be find    declare var i_rc=$RC_ERR    declare var i_tmp=$RC_ERR    declare var str_rc=""    # printf "cmd = [%s] [%s]\n" "$1" "$2"    while true    do        # find sub string        str_rc=$(echo -e "$1" | grep -q "$2")        i_tmp=$?        if [ $i_tmp -ne $RC_OK ]        then            # not find sub string            # printf "not find the sub string\n"            break        fi        # printf "found sub string\n"        i_rc=$RC_OK        break    done    return $i_rc}function check_return_code() {    # $1 is tip    # $2 is err message code    # $3 is err message string    declare var i_rc=$RC_ERR    declare var i_tmp=$RC_ERR    while true    do        # printf "%s : return code = [%d], return msg = [%s]\n"  "$1" $2 "$3"        printf "%s\n"  "$1"        # compare value        if [ $2 -ne $RC_OK ]        then            # printf "bp1\n"            break        fi        # find sub string        find_sub_string "$3" "$ERR_CMD_NOT_FOUND"        i_tmp=$?        # printf "i_tmp = %d\n" $i_tmp        if [ $i_tmp -ne $RC_OK ]        then            i_rc=$RC_OK            # printf "bp2\n"        fi        break    done    return $i_rc}function main() {    declare var b_ok=false    declare var b_rc=false    declare var i_rc=RC_ERR    declare var str_rc=""    declare var i_pos=0    while true    do         clear_screen        show_version        export LC_ALL=C        # 假设基本命令都有 e.g. echo print head cut readlink        # --------------------------------------------------------------------------------        # check bash        # --------------------------------------------------------------------------------        # bash --version | head -n1 | cut -d" " -f1-10        str_rc=$( { bash --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check bash ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : bash not found\n"            break        fi        str_rc=$( bash --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check /bin/sh        # --------------------------------------------------------------------------------        # echo "/bin/sh -> `readlink -f /bin/sh`"        str_rc=$( { readlink -e /bin/sh; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check /bin/sh ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : /bin/sh not found\n"            break        fi        str_rc=$( readlink -e /bin/sh )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check Binutils        # --------------------------------------------------------------------------------        # echo -n "Binutils: "; ld --version | head -n1 | cut -d" " -f3-        str_rc=$( { ld --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check Binutils ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : Binutils not found\n"            break        fi        str_rc=$( ld --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check bison        # --------------------------------------------------------------------------------        # bison --version | head -n1        str_rc=$( { bison --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check bison ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : bison not found\n"            break        fi        str_rc=$( bison --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check yacc        # --------------------------------------------------------------------------------        # -h symboliclink         if [ -h /usr/bin/yacc ]        then            # echo "/usr/bin/yacc -> `readlink -f /usr/bin/yacc`";            str_rc=$( { readlink -e /usr/bin/yacc; } 2>&1 )            i_rc=$?        # yum install byacc        #  -x executablefile        elif [ -x /usr/bin/yacc ]        then            # echo yacc is `/usr/bin/yacc --version | head -n1`            str_rc=$( { /usr/bin/yacc -V | head -n1; } 2>&1 )            i_rc=$?        else            str_rc=$ERR_CMD_NOT_FOUND            i_rc=$RC_ERR        fi        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check yacc ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : yacc not found\n"            break        fi        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check bzip2        # --------------------------------------------------------------------------------        # bzip2 --version 2>&1 < /dev/null | head -n1 | cut -d" " -f1,6-        # bzip2 can't write compressed data to a terminal        str_rc=$( bzip2 --version 2>&1 < /dev/null )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check bzip2 ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : bzip2 not found\n"            break        fi        str_rc=$( bzip2 --version 2>&1 < /dev/null | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check Coreutils        # --------------------------------------------------------------------------------        # echo -n "Coreutils: "; chown --version | head -n1 | cut -d")" -f2        str_rc=$( { chown --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check Coreutils ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : chown not found\n"            break        fi        str_rc=$( chown --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check diff        # --------------------------------------------------------------------------------        # diff --version | head -n1        str_rc=$( { diff --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check diff ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : diff not found\n"            break        fi        str_rc=$( diff --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check find        # --------------------------------------------------------------------------------        # find --version | head -n1        str_rc=$( { find --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check find ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : find not found\n"            break        fi        str_rc=$( find --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check gawk        # --------------------------------------------------------------------------------        # gawk --version | head -n1        str_rc=$( { gawk --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check gawk ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : find not found\n"            break        fi        str_rc=$( gawk --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check awk        # --------------------------------------------------------------------------------        if [ -h /usr/bin/awk ]        then            # echo "/usr/bin/yacc -> `readlink -f /usr/bin/awk`";            str_rc=$( { readlink -e /usr/bin/awk; } 2>&1 )            i_rc=$?        elif [ -x /usr/bin/awk ]        then            # echo awk is `/usr/bin/awk --version | head -n1`            str_rc=$( { /usr/bin/awk --version | head -n1; } 2>&1 )            i_rc=$?        else            str_rc=$ERR_CMD_NOT_FOUND            i_rc=$RC_ERR        fi        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check awk ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : awk not found\n"            break        fi        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check gcc        # --------------------------------------------------------------------------------        # gcc --version | head -n1        str_rc=$( { gcc --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check gcc ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : gcc not found\n"            break        fi        str_rc=$( gcc --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check g++        # --------------------------------------------------------------------------------        # g++ --version | head -n1        str_rc=$( { g++ --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check g++ ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : g++ not found\n"            break        fi        str_rc=$( g++ --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check ldd        # --------------------------------------------------------------------------------        # ldd --version | head -n1 | cut -d" " -f2- # glibc version        str_rc=$( { g++ --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check ldd ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : ldd not found\n"            break        fi        str_rc=$( ldd --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check grep        # --------------------------------------------------------------------------------        # grep --version | head -n1        str_rc=$( { grep --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check grep ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : grep not found\n"            break        fi        str_rc=$( grep --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check gzip        # --------------------------------------------------------------------------------        # gzip --version | head -n1        str_rc=$( { gzip --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check gzip ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : gzip not found\n"            break        fi        str_rc=$( gzip --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check Linux Kernel        # --------------------------------------------------------------------------------        # cat /proc/version        str_rc=$( { cat /proc/version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check Linux Kernel ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : Linux Kernel not found\n"            break        fi        str_rc=$( cat /proc/version )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check m4        # --------------------------------------------------------------------------------        # m4 --version | head -n1        str_rc=$( { m4 --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check m4 ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : m4 not found\n"            break        fi        str_rc=$( m4 --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check make        # --------------------------------------------------------------------------------        # make --version | head -n1        str_rc=$( { make --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check make ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : make not found\n"            break        fi        str_rc=$( make --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check patch        # --------------------------------------------------------------------------------        # patch --version | head -n1        str_rc=$( { patch --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check patch ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : patch not found\n"            break        fi        str_rc=$( patch --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check perl        # --------------------------------------------------------------------------------        # echo Perl `perl -V:version`        str_rc=$( { perl --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check perl ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : perl not found\n"            break        fi        str_rc=$( { perl -V:version; } | head -n1 )        printf "\t[ok]. %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check sed        # --------------------------------------------------------------------------------        # sed --version | head -n1        str_rc=$( { sed --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check sed ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : sed not found\n"            break        fi        str_rc=$( sed --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check tar        # --------------------------------------------------------------------------------        # tar --version | head -n1        str_rc=$( { tar --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check tar ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : tar not found\n"            break        fi        str_rc=$( tar --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check makeinfo        # --------------------------------------------------------------------------------        # yum -y install texinfo        # makeinfo --version | head -n1        str_rc=$( { makeinfo --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check makeinfo ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : makeinfo not found\n"            break        fi        str_rc=$( makeinfo --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check xz        # --------------------------------------------------------------------------------        # xz --version | head -n1        str_rc=$( { xz --version; } 2>&1 )        i_rc=$?        i_pos=$(( $i_pos + 1 ))        check_return_code "[$i_pos] check xz ..." $i_rc "$str_rc"        i_rc=$?        if [ $i_rc -ne $RC_OK ]        then            printf "\terror : xz not found\n"            break        fi        str_rc=$( xz --version | head -n1 )        printf "\t[ok] %s\n" "$str_rc"        # --------------------------------------------------------------------------------        # check g++ compilation        # --------------------------------------------------------------------------------        i_pos=$(( $i_pos + 1 ))        printf "[$i_pos] check g++ compilation\n"         echo -e 'int main() { return 0; }' > dummy.c && g++ -o dummy dummy.c        if [ -x dummy ]        then             echo -e "\t[ok] g++ compilation success"        else            echo -e "\terror : g++ compilation failed"            rm -f dummy.c dummy            break        fi        rm -f dummy.c dummy        # --------------------------------------------------------------------------------        # check lib depend        # mpfr, gmp and mpc        #        # if no mpfr.la, install mpfr        # wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2        # bunzip2 mpfr-2.4.2.tar.bz2        # tar xvf mpfr-2.4.2.tar        # cd mpfr-2.4.2        # ./configure --disable-shared --enable-static --prefix=/usr --with-gmp=/usr        # make && make check && make install        # now have file : /usr/lib/libmpfr.la        #        # yum search gmp        # yum -y install gmp-devel.x86_64        # but after install, can't find libgmp.la, so must build and install it from source code        #        # wget ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2        # bunzip2 gmp-4.3.2.tar.bz2        # tar xvf gmp-4.3.2.tar        # cd gmp-4.3.2        # ./configure --disable-shared --enable-static --prefix=/usr        # make && make check && make install        #        # install mpc        # wget ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz        # tar -xzvf mpc-0.8.1.tar.gz        # cd mpc-0.8.1        # ./configure --disable-shared --enable-static --prefix=/usr --with-gmp=/usr --with-mpfr=/usr        # make && make check && make install        # --------------------------------------------------------------------------------        printf "check lib depend ...\n"        b_rc=true        for lib in lib{mpfr,mpc,gmp}.la        do            i_pos=$(( $i_pos + 1 ))            printf "[%d] check lib [%s]...\n" $i_pos "$lib"            # find 的最后一个参数,只能用双引号围起来, 否则找不到确实存在的文件            str_rc=$( { find /usr/lib* -name "$lib"; } )            # printf "str_rc = [%s]\n" "$str_rc"            if [ -z $str_rc ]            then                printf "\terror : not find [%s]\n" "$lib"                b_rc=false                break;            else                printf "\tok : find [$lib] = [%s]\n" "$str_rc"            fi        done        unset lib        if [ "$b_rc" = false ]        then            # some lib not find            break        fi        # --------------------------------------------------------------------------------        # check all ok        # --------------------------------------------------------------------------------        b_ok=true        break    done    if [ "$b_ok" = true ]    then        echo -e "env check success"    else        echo -e "env check failed"    fi}mainexit 0

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

上一篇:wireshark的使用方法整理
下一篇:You have loaded library .so which might have disabled stack guard

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月18日 08时34分12秒