简体   繁体   English

测试和记录终端输出

[英]Test and log terminal output

I am using the following command line in order to send command to my device controller thru bluetoothctl tool. 我使用以下命令行,以通过bluetoothctl工具将命令发送到我的设备控制器。

#!/bin/sh
{ sleep 1 && echo -e 'power on \n' && sleep 1;} | bluetoothctl

Which returns in the terminal : 哪个在终端返回:

root@snp:~# ./bt.sh 
[NEW] Controller 01:02:03:04:05:C3 snp [default]
[NEW] Device 98:0D:2E:E5:14:81 HTC
Changing power on succeeded
[CHG] Controller 01:02:03:04:05:C3 Powered: yes
[DEL] Controller 01:02:03:04:05:C3 snp [default]

I would like to reduce and manage the number of messages returned. 我想减少和管理返回的消息数量。 Basically, I want to display on screen and store in a log file : 基本上,我想在屏幕上显示并存储在日志文件中:

Changing power on succeeded
[CHG] Controller 01:02:03:04:05:C3 Powered: yes

I enter 我进去了

{ sleep 1 && echo -e 'power on \n' && sleep 1;} | bluetoothctl | grep -E "Changing|[CHG]" 2>&1 | tee log.txt

But I obtain, 但我得到了,

root@snp:~# ./bt.sh 
[NEW] Controller 01:02:03:04:05:C3 snp [default]
[NEW] Device 01:02:03:04:05:C3 HTC 
Changing power on succeeded
[DEL] Controller 01:02:03:04:05:C3 snp [default]

Where is my fault ? 我的错在哪里?

In addition, is it possible to test if Powered = yes or not thru a conditional test, and all stuff do in the same line ? 此外,是否有可能通过条件测试来测试Powered =是或否,并且所有内容都在同一行中进行测试?

-------------------------------- --------------------------------

Edit n°1 : 编辑n°1:

Indeed, the status messages are not directed to standard error, 2>&1 was useless. 实际上,状态消息并不是针对标准错误,2>&1是无用的。

However, I tried your proposition by means of : 但是,我通过以下方式尝试了你的命题:

{ sleep 1 && echo -e 'power on \n' && sleep 1;} | bluetoothctl | grep -E 'Changing|\[CHG]' 

which always returns 总是回来

root@snp:~# ./bt.sh 
Changing power on succeeded

Instead of 代替

root@snp:~# ./bt.sh 
Changing power on succeeded
[CHG] Controller 01:02:03:04:05:C3 Powered: yes

The regex [CHG] is a character class which matches any one of C , H , or G . 正则表达式[CHG]是一个匹配CHG中任何一个的字符类。 If you want to match an opening square bracket literally, you have to escape it. 如果你想在字面上匹配一个开头方括号,你必须逃避它。

grep -E 'Changing|\[CHG]'

In addition, you might want to check whether the status messages are being directed to standard error. 此外,您可能希望检查状态消息是否定向到标准错误。 If so, try 如果是这样,试试吧

... | bluetoothctl 2>&1 | grep ...

The grep 2>&1 redirection you have does not seem to serve any useful purpose. 您拥有的grep 2>&1重定向似乎没有任何用处。

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

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