bash脚本:从串行端口(的/ dev / ttyUSB0)保存流文件,直到特定输入(例如EOF)出现 [英] Bash script: save stream from Serial Port (/dev/ttyUSB0) to file until a specific input (e.g. eof) appears

查看:348
本文介绍了bash脚本:从串行端口(的/ dev / ttyUSB0)保存流文件,直到特定输入(例如EOF)出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个bash脚本从串口数据流中读取(RS232转USB适配器 - 端口是:/ dev / ttyUSB0)。该数据应存储逐行在一个文件中,直到一个特定的输入(例如,EOF)出现。我可以给任何外部输入到串行端口。
直到现在我用猫来读取数据,工作正常。

I need a bash script to read the data stream from a Serial Port (RS232 to USB adapter - Port: /dev/ttyUSB0). The data should be stored line by line in a file until a specific input (for example "eof") appears. I can give any external input to the Serial Port. Till now I use cat to read the data, which works fine.

cat /dev/ttyUSB0 -> file.txt

问题是,我需要通过输入可逆计数器+ C来完成命令自己,但我不知道什么时候数据流结束,ttyUSB0文件不gerenate一个EOF。我想这个实施自己,但没有找到一个方便的解决方案。
下面的命令工作,但我不知道如何使用它我的问题(世界将创造一个错误找不到命令):

The problem is, that I need to finish the command myself by entering cntr+C, but I don't know exactly when the data stream ends and the ttyUSB0 file does not gerenate an EOF. I tried to implement this myself, but did not find a convenient solution. The following command works, but I don't know how to use it for my problem ("world" will create a "command not found" error):

#!/bin/bash
cat > file.txt << EOF
hello
EOF
world

以下code适用于我的问题,但它需要太多的时间(数据流由〜2万行):

The following code works for my problem, but it takes too much time (the data stream consists of ~2 million lines):

#!/bin/bash
while read line; do
     if [ "$line" != "EOF" ]; then
          echo "$line" >> file.txt
     else
          break
     fi
done < /dev/ttyUSB0

有没有人对我的问题的可能性方便?

Has anyone a convenient possibility for my problem?

推荐答案

尝试 AWK(1)

awk `
/EOF/ {exit;} 
 {print;}` < /dev/ttyUSB0 > file.txt

这将停止时,看到该行 EOF 并打印其他一切 file.txt的

This stops when it sees the line EOF and prints everything else to file.txt

这篇关于bash脚本:从串行端口(的/ dev / ttyUSB0)保存流文件,直到特定输入(例如EOF)出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆