Test and log terminal output
I am using the following command line to send a command to my device controller via the bluetoothctl tool.
#!/bin/sh
{ sleep 1 && echo -e 'power on \n' && sleep 1;} | bluetoothctl
Which returns to 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 managed messages. 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
Input
{ sleep 1 && echo -e 'power on \n' && sleep 1;} | bluetoothctl | grep -E "Changing|[CHG]" 2>&1 | tee log.txt
But I get,
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?
Also, is it possible to check if power = yes or no through conditional test and all that is done in one line?
--------------------------------
Change n Β° 1:
Indeed, the status messages are not directed to standard error, 2> and 1 are useless.
However, I tried your suggestion with:
{ 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
root@snp:~# ./bt.sh
Changing power on succeeded
[CHG] Controller 01:02:03:04:05:C3 Powered: yes
source to share
A regular expression [CHG]
is a character class that matches any of C
, H
or G
. If you want to literally match the square bracket, you need to escape it.
grep -E 'Changing|\[CHG]'
Additionally, you can check if status messages are being routed to standard error. If yes try
... | bluetoothctl 2>&1 | grep ...
The redirect grep 2>&1
you have doesn't seem to serve any useful purpose.
source to share