• 使用shell脚本简单模拟对特定文件同时读写操作



    使用shell脚本简单模拟对特定文件同时读写操作
    文件内容的格式:
    field1    ,       field2    , field3    ,       field4
    以,为分隔符,但是存在空格。

    • 脚本用法如下:

    ./check_write_read.sh 10

    • 输出结果:

    Thu Apr 27 19:59:44 CST 2017:Read operation finished
    670
    Thu Apr 27 19:59:44 CST 2017:Write operation finished
    671
    Thu Apr 27 19:59:46 CST 2017:Check Write OK
    Thu Apr 27 19:59:56 CST 2017:Check Read OK

    • 脚本内容:
    if [[ $1 =~ ^[1-9] ]]; then
       timeInterval=$1
    else
       echo "parameter error, should be number"
       exit 1
    fi
    timefrom_1=`date +%s`
    timeto_1=`expr $timefrom_1 + $timeInterval`
    timenow_1=$timefrom_1
    count_1=0
    
    timefrom_2=`date +%s`
    timeto_2=`expr $timefrom_2 + $timeInterval`
    timenow_2=$timefrom_2
    count_2=0
    
    function rand(){
        min=1
        max=292
        num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
        echo $(($num%$max+$min))
    }
    
    function checkWrite(){
     while read LINE
     do
        resultMsg=`cat configdata.cfg | grep $LINE`
        if [ -z "$resultMsg" -o "$resultMsg" == "" -o x"$resultMsg" == x ]; then
               echo "write error"
           echo "LINE:$LINE"
           echo "resultMsg:$resultMsg"
           exit 1
        fi
     done<result_write.txt
     echo "`date`:Check Write OK"
    }
    
    function checkRead(){
     while read LINE
     do
        linenumber=`echo $LINE | cut -d : -f1`
        linevalue=`echo $LINE | cut -d : -f2 | sed s/[[:space:]]//g`
        resultMsg=`sed -n "$linenumber,1p" configdata.cfg | awk -F ',' 'BEGIN{OFS=","}{print $1,$2}' | sed s/[[:space:]]//g`
        resultMsg=`sed -n "$linenumber,1p" configdata.cfg | cut -d , -f1,2 |sed s/[[:space:]]//g`
        if [ -z "$resultMsg" -o "$resultMsg" == "" -o x"$resultMsg" == x ]; then
                echo "read error,it's null"
            echo "linenumber:$linenumber"
            echo "linevalue:$linevalue"
            echo "resultmsg:$resultMsg"
            exit 1
        fi
    
        if [ "$resultMsg" != "$linevalue" ]; then
               echo "read error,value error"
           echo "resultMsg:$resultMsg"
           echo "linevalue:$linevalue"
           exit 1
        fi
        #echo "resultMsg:$resultMsg"
        #echo "linevalue:$linevalue"
     done<result_read.txt
     echo "`date`:Check Read OK"
    }
    
    #write operation
    function writeOperation(){
       rm -f result_write.txt
       until [ "$timenow_1" == "$timeto_1" ]
       do
               rnd_1=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
              echo "${rnd_1}">>result_write.txt
              echo "${rnd_1}">>configdata.cfg
              count_1=`expr $count_1 + 1`
               timenow_1=`date +%s`
       done
       echo "$count_1" | tee -a result_write.txt
       echo "`date`:Write operation finished"
    }
    #read action
    function readOperation(){
       rm -f result_read.txt
       until [ "$timenow_2" == "$timeto_2" ]
       do
           rnd_2=`rand`
           line="${rnd_2}"":""`sed -n "$rnd_2,1p" configdata.cfg | cut -d , -f1,2`"
           echo $line>>result_read.txt
           timenow_2=`date +%s`
       done
       echo "`date`:Read operation finished"
    }
    
    {
       writeOperation
    }&
    
    {
       readOperation
    }&
    
    wait
    #sleeptime=`expr $timeInterval + 2`
    #sleep $sleeptime
    cat result_write.txt | wc -l | tee
    checkWrite
    checkRead
    exit 0
  • 相关阅读:
    Redis安装配置
    Git本地服务器搭建
    JDK安装配置
    ssh免密登录
    设计模式
    IDEA 快捷键
    LeetCode Sliding Window Maximum
    ElasticSearch 使用小结
    LeetCode Product of Array Except Self
    LeetCode Delete Node in a Linked List
  • 原文地址:https://www.cnblogs.com/gracejiang/p/6776282.html
Copyright © 2020-2023  润新知