shell脚本实例

Posted Hsin wang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本实例相关的知识,希望对你有一定的参考价值。

#!/bin/bash

SRC_PATH=/mnt/source/xx/xx

# Merge all spec file for quick search
mergedSpec="./allSpec-"$(date +%Y%m%d).txt

function merge_all_specs(){
    allSpec=$(find $SRC_PATH -name *.spec)
    for specFile in $allSpec
    do  
        specName=$(basename $specFile)
        specPrefix=${specName%.*} 
        #echo $specName, echo ${specName%.*}
        prefix="${specPrefix}":"${specFile}:"
        #echo $prefix
        prefix=${prefix//\//\\\/}
        #echo $prefix
        cat $specFile | sed s/^/‘"$prefix"‘&/g >> $mergedSpec 
    done
}

if [ ! -f "$mergedSpec" ]; then
    merge_all_specs
fi

resultFile=$1-confirmed
:>$resultFile

cat $1 | while read LINE
do
    #echo $LINE | grep -ri CVE
    #if [ $? -ne 0 ]
    #then
    #    continue
    #fi

    sa=`echo $LINE | awk {print $1}`
    cves=`echo $LINE | awk {for (i=2 ;i<=NF;i++) printf $i " "}`

    for cve in $cves
    do
        #echo $cve
        grep -i $cve $mergedSpec >/dev/null 2>&1
        if [ $? -ne 0 ]
        then
            echo -e "$sa $cve:\t待确认"
            echo -e "$sa $cve:\t待确认" >> $resultFile
        else
                record=$(grep -ri $cve $mergedSpec)
        specFile=`echo ${record} | awk -F : {print $2}`

        comm_dir=`dirname $specFile`
                comm_specfile=`basename $specFile`

        comm=${comm_specfile%.*}
        #echo ${comm_dir}"->"${comm_specfile}

        if [ ! -d "${comm_dir}" ]
                then
                     continue
                fi
pushd ${comm_dir} >/dev/null 2>&1

                if [ -d ".git" ]
                then
                     commitmsg=`git blame ${comm_specfile} | grep -i $cve | head -1 | awk {print $1}`
                     #echo "----ID---"${commitmsg}
                else
                     echo -e "$sa $cve:\tNo find .git"
             popd >/dev/null 2>&1
                     continue
                fi
popd >/dev/null 2>&1

                # Remove all space
            version=`echo $version | sed s/[[:space:]]//g`
            release=`echo $release | sed s/[[:space:]]//g`

                # Special Cases


            echo -e "$sa $cve:\tMerged","\t"${comm%%:*}-${version##*:}-${release##*:}
            echo -e "$sa $cve:\tMerged","\t"${comm%%:*}-${version##*:}-${release##*:} >> $resultFile
        fi
    done
done

 

以上是关于shell脚本实例的主要内容,如果未能解决你的问题,请参考以下文章

用于确保在任何给定时间仅运行一个 shell 脚本的 shell 片段 [重复]

常用python日期日志获取内容循环的代码片段

Linux - 通过操作文件锁来实现shell script进程单实例

shell脚本小实例

实例快速上手shell脚本

shell脚本引用expect