shell脚本将数据解析为变量然后更新数据库

时间:2021-04-03 23:35:28

I have a device on my network that is posting data to an html page. I need to be able to collect the data from the page and insert it into my database so I can have historical reference to the data. I am able to use the following command to retrieve the fields from the site. The result returns three numbers separated by a line break.

我的网络上有一个设备将数据发布到html页面。我需要能够从页面收集数据并将其插入到我的数据库中,以便我可以对数据进行历史参考。我可以使用以下命令从站点中检索字段。结果返回由换行符分隔的三个数字。

I would like to run this as a linux cron job, so I would like to use a linux script to:

我想将它作为linux cron工作运行,所以我想使用linux脚本:

  1. save the data (numbers) to variables
  2. 将数据(数字)保存到变量中
  3. connect to my database and insert the data in a table
  4. 连接到我的数据库并将数据插入表中

My command line to retrieves the information is below. This works great for parsing out the data from the device:

我的命令行检索信息如下。这非常适合解析设备中的数据:

 curl -s http://local_device.com/dtm.html?address=C5:0 | grep -Po '[0-9]+(?=[^0-9]+(C5:2<|C5:6<|C5:13))'

1 个解决方案

#1


1  

see below for a reference, since they are numbers, I did not quote them:
set -- $(curl -s http://local_device.com/dtm.html?address=C5:0 | grep -Po '[0-9]+(?=[^0-9]+(C5:2<|C5:6<|C5:13))')
C5_2=$1
C5_6=$2
C5_13=$3
mysql -e "
   INSERT INTO db.table VALUES ($C5_2, $C5_6, $C5_13) 
"

#1


1  

see below for a reference, since they are numbers, I did not quote them:
set -- $(curl -s http://local_device.com/dtm.html?address=C5:0 | grep -Po '[0-9]+(?=[^0-9]+(C5:2<|C5:6<|C5:13))')
C5_2=$1
C5_6=$2
C5_13=$3
mysql -e "
   INSERT INTO db.table VALUES ($C5_2, $C5_6, $C5_13) 
"