使用shell指令碼簡單類比對特定檔案同時讀寫操作

來源:互聯網
上載者:User

標籤:else   shell指令碼   amp   ltm   exit   讀寫   random   ini   and   


使用shell指令碼簡單類比對特定檔案同時讀寫操作
檔案內容的格式:
field1    ,       field2    , field3    ,       field4
以,為分隔字元,但是存在空格。

 

  • 指令碼用法如下:

./check_write_read.sh 10

  • 輸出結果:

Thu Apr 27 19:59:44 CST 2017:Read operation finished670Thu Apr 27 19:59:44 CST 2017:Write operation finished671Thu Apr 27 19:59:46 CST 2017:Check Write OKThu Apr 27 19:59:56 CST 2017:Check Read OK

  • 指令碼內容:
if [[ $1 =~ ^[1-9] ]]; then   timeInterval=$1else   echo "parameter error, should be number"   exit 1fitimefrom_1=`date +%s`timeto_1=`expr $timefrom_1 + $timeInterval`timenow_1=$timefrom_1count_1=0timefrom_2=`date +%s`timeto_2=`expr $timefrom_2 + $timeInterval`timenow_2=$timefrom_2count_2=0function 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 operationfunction 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 actionfunction 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 $sleeptimecat result_write.txt | wc -l | teecheckWritecheckReadexit 0

使用shell指令碼簡單類比對特定檔案同時讀寫操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.