標籤: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指令碼簡單類比對特定檔案同時讀寫操作