简体   繁体   English

使用C ++或Bash / Shell脚本评估后台linux命令的输出

[英]Evaluate output of a background linux command with C++ or Bash/Shell Script

Question: Using C++ or a bash/shell script, how can I evaluate output of a long running linux process? 问题:使用C ++或bash / shell脚本,如何评估长时间运行的linux进程的输出?

Example: 例:

root@example.com~# iw event
(This command will run until manually killed.) 
(It will output data that I will want to read and parse line by line.)

What is the most efficient way to evaluate the std output of this command when a new line is added to its buffer? 将新行添加到其缓冲区时,评估此命令的std输出的最有效方法是什么?

For example: iw event will output a line that says: 例如:iw event将输出一行内容:

new station: 0e:0e:20:2d:20

I want to detect "new station" and run another command with the mac address. 我想检测“新站”并使用mac地址运行另一个命令。 IE: IE浏览器:

./myProgram -mac 0e:0e:20:2d:20

Thanks! 谢谢!

If you run the command as shown, all output will go to stdout and display on the terminal. 如果按如下所示运行命令,则所有输出将转到stdout并显示在终端上。 To capture the output you have a few options: 要捕获输出,您有几种选择:

  • Pipe the output to your monitor program, as in iw events | yourmonitorprogram iw events | yourmonitorprogram一样,将输出通过管道传递到您的监视程序iw events | yourmonitorprogram iw events | yourmonitorprogram which then reads stdin. iw events | yourmonitorprogram ,然后读取标准输入。 iw should probably be modified to use unbuffered output. iw可能应该修改为使用无缓冲输出。
  • Write the output of iw to a file and then use the same technique as the tail -f command to poll the file periodically iw的输出写入文件,然后使用与tail -f命令相同的技术定期轮询文件
  • Have iw write to a named pipe or socket and have your monitor program read from that pipe or socket. iw写入命名的管道或套接字,并从该管道或套接字读取监视程序。 This option requires modification to iw . 此选项需要修改iw

The simplest option is the first one 最简单的选择是第一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM