使用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
时间: 2024-08-02 02:45:16

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

crontab里shell脚本将top信息写入文件

crontab里shell脚本将top信息写入文件: 注: 1.top -n 1代表执行1次退出(默认top是不退出的),-d 1代表每1秒执行1次 2.crontab里需加/bin/bash # crontab -e */5 * * * * /bin/bash /usr/local/bin/top.sh # vi top.sh #!/bin/bash Date=`date +%Y%m%d` Date2=`date +%Y%m%d_%H%M` Date3=`date +%Y%m%d -d "7

【Python】Python对文件的读写操作

刚刚接触Python,感觉其对文件的操作还是很方便的.下面是我入门Python对文件操作的一个简单程序,希望对初学者有所帮助. test.py def processFile(inputFile, outputFile): #定义一个函数 fin = open(inputFile, 'r') #以读的方式打开文件 fout = open(outputFile, 'w') #以写得方式打开文件 for eachLine in fin: #读取文件的每一行 line = eachLine.strip

(六)kernel中文件的读写操作可以使用vfs_read()和vfs_write

需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() filp_close(), vfs_read() vfs_write(),set_fs(),get_fs()等,这些函数在linux/fs.h和asm/uaccess.h头文件中声明.下面介绍主要步骤 1. 打开文件 filp_open()在kernel中可以打开文件,其原形如下: strcut file

Java 对不同类型的数据文件的读写操作整合器[JSON,XML,CSV]-[经过设计模式改造](2020年寒假小目标03)

日期:2020.01.16 博客期:125 星期四 我想说想要构造这样一个通用文件读写器确实不容易,嗯~以后会添加更多的文件类型,先来熟悉一下文件内容样式: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beangroup> 3 <javabean> 4 <data name='code'>A001</data> 5 <data name='name'>

IO流文件的读写操作

字符流有两个抽象类:Writer   Reader.其对应子类FileWriter,FileReader可实现文件的读写操作 同样,字节流也有两个抽象类:InputStream OutputStream.其对应子类有FileInputStream,FileOutputStream可实现文件读写 IO流中的重要方法:read()方法返回-1,readLine方法返回null.用法列如:while((line=br.readLine())!=null).Scanne类中的hasNext()方法如果此

File --文件的读写操作

File --文件的读写操作------------------- 1. 通过数组实现对一组数据的写入 1 package day01; 2 3 import java.io.FileNotFoundException; 4 import java.io.IOException; 5 import java.io.RandomAccessFile; 6 7 /** 8 * 批量写入一组字节 9 * @author Administrator 10 * 11 */ 12 public class

INI 文件的读写操作

在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static readonly string strFilePath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "App.ini";//INI文件路径 #endregion #region 私有方法 /// <summary> /

C# 运用StreamReader类和StreamWriter类实现文件的读写操作

对文件的读写操作应该是最重要的文件操作,System.IO命名空间为我们提供了诸多文件读写操作类,在这里我要向大家介绍最常用也是最基本的StreamReader类和StreamWriter类.从这两个类的名称我们不难发现它们都是基于流的读写操作类. 我们可以通过File类的OpenText()方法来获取一个StreamReader对象,通过该对象我们可以实现对文本文件的读操作,方法如下:  Console.WriteLine("Reading the contents from the file

C/C++关于文件的读写操作以及文件的打开和保存

通过近来的学习,总结一下关于文件的读写操作以及文件的打开和保存的方法. 一.文件的读写操作: (1)C语言对文件的读写操作 在C语言中,对文件的读写操作是用FILE结构体和常用的对文件操作的函数实现的,下面总结一下C语言中对文件操作的常用函数: fopen()   打开以文件名指定的文件 fwrite()  写文件 fread()  读文件 fseek()  移动文件的指针到新的位置通过该函数的参数设定的偏移量和初始位置 rewind()  移动文件的指针到文件流的开始位置,在通常情况下可用fs