一个读取文件内容的shell脚本

如下展示的是一个shell脚本,该脚本可以2行2行的读取文件中的内容,然后把读取到的文件内容保存到一个名为target.json的文件当中,然后打印出target.json中的内容,打印完毕删除target.json文件,然后再新建target.json文件,然后再读入两行内容,然后再打印,再删除,直到源文件被读完。贴到博客的目的是做个笔记,毕竟本人没有专门学个shell编程,这些脚本中的某些语法可能后续会用到,所以算是做了个储备。

#!/bin/bash

count=0
rm target.json
touch target.json


while read line;do

((count++))

{
        echo $line >> target.json

        if [ $count -ge 2 ] && [ $((count%2)) -eq 0 ];then
                sudo cat target.json
                echo "2 line is reached!, current count is:"$count
                count=0
                sudo rm -f target.json
                touch target.json
        fi

}
      
done < $1
echo 'last submit'
    

假设保存该脚本的文件名是readLine.sh。则用法是: ./readLine.sh + 目标文件名。 该脚本改编自如下脚本,原文链接是: 原始脚本来源,原脚本顺便也张贴出来:

#!/bin/bash

count=0
rm target.json
touch target.json


while read line;do

((count++))

{
        echo $line >> target.json

        if [ $count -gt 100000 ] && [ $((count%2)) -eq 0 ];then
                count=0
                curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null
                rm target.json
                touch target.json
        fi

}

done < $1
echo 'last submit'
curl -XPOST localhost:9200/_bulk --data-binary @target.json > /dev/null



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值