0

I need to write to file output of two commands running together in one terminal, as example above, OR logging the output from the first, when the second is also runnign - how to do it?

sudo btmon ; sudo hcitool lescan

I tried something like

{ sudo btmon ; sudo hcitool lescan ;} > file.txt but id didn't give out of both. As work around I run them in two different terminals

sudo btmon > file.txt from one

and

sudo hcitool lescan from another

and it worked in a way I accept, I had the need log from the first command. But I want to have it all in one terminal with just a kind of one string and I know it is possible. The only question is how it is done?

  • The { sudo btmon ; sudo hcitool lescan ;} > file.txt approach is actually correct and should work. Only thing I can imagine is that these tools log to STDERR instead of STDOUT. In that case simply try &> instead of > to redirect both streams. Does that work? – Byte Commander Jul 16 '17 at 15:22
  • @ByteCommander if i run sudo btmon & sudo hcitool lescan } &> scan_log.txt the only log of second command is presented – Ruslan Gerasimov Jul 16 '17 at 15:26
  • Do you see the other command print anything to the terminal that is not captured in the file? But it actually should work this way, unless the first command maybe detects that it doesn't output to the terminal and changes something then. No idea. – Byte Commander Jul 16 '17 at 15:50
  • @ByteCommander I have found the right way after all using your advice, it worked as (sudo btmon & sudo hcitool lescan ) &> scan_log.txt fixed it to parenthesizes . So if you make it as an answer then I will vote and accept. Thanks – Ruslan Gerasimov Jul 16 '17 at 16:17
  • You can answer your own question yourself too. After all, you tried the round braces :) – Byte Commander Jul 16 '17 at 17:15
4

Since nobody seemed to create an answer I'll do it. I'll use extracts of this source

The solution in the comments

(sudo btmon & sudo hcitool lescan ) &> scan_log.txt

It uses () instead of {}. Also the &> means that not only STDOUT but also STDERR will be forwarded. As it seems one of those tools used STDERR for output so it was needed. I want to explain the problems from the comments a bit therefore I'll explain the difference between () and {} first.

(command)

Placing a list of commands between parentheses causes a subshell to be created, and each of the commands in list to be executed in that subshell, without removing non-exported variables.

Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

{ command; }

Placing a list of commands between curly braces causes the list to be executed in the current shell context. No subshell is created. The semicolon (or newline) following list is required.

In addition to the creation of a subshell, there is a subtle difference between these two constructs due to historical reasons. The braces are reserved words, so they must be separated from the list by blanks. The parentheses are operators, and are recognized as separate tokens by the shell even if they are not separated from the list by whitespace.

Looking back to the first suggestion the OP simply forgot the ; because The semicolon (or newline) following list is required. between {} so both of the following solutions should solve the problem:

{ sudo btmon; sudo hcitool lescan; } &> scan_log.txt

(sudo btmon & sudo hcitool lescan) &> scan_log.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.