Write to file output of two commands running together in one terminal
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?
81 Answer
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.txtIt 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