Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The attached snippet processes hcitool scan, with options and REDIRECT the standard output to BOTH screen / console ( Eclipse IDE term ) and system file (temp_file.txt ).
1. Why is it necessary to add 2 into redirection ?
Why not ">@1" ?
2. Without the "pipe | " hcitool complains about "too many parameters".
But wants / suggests "zero parameters "(?)
3. position of "tee" is also critical , AFTER the "|" and before the file.
4. When no output to console is needed , this works just fine

system("hcitool scan --info --class > temp_file.txt ");


C++
system("hcitool scan --info  --class 2>&1 | tee temp_file.txt ");


What I have tried:

Removing "2"
Removing "|"
Moving tee around.
Basically I need better / some explanations so blind "hit and miss"
coding is avoided.
Appreciate any on the subject comments.
Posted
Updated 1-Mar-20 6:18am

1 solution

That is nothing to do with HCI it is standard Linux shell scripting. The "2>&1" tells the shell to send all output that would normally go to stderr to stdout. The "|" (pipe symbol) says to pass all output (i.e the combined stderr and stdout) to the tee command which sends it to the terminal and also the named file. This and other shell features are all described in full detail in the man page for bash.
 
Share this answer
 
Comments
Vaclav_ 1-Mar-20 12:25pm    
stderr to stdout. The "|" (pipe symbol) says to pass all output (i.e the combined stderr and stdout
BUT at that point the stderr was redirected to stdout so there in no stderr as "all output" so what is being commbined / piped ?
Richard MacCutchan 1-Mar-20 12:37pm    
It just means that the two streams are interleaved and passed through the pipe as a single stream. So you will see any error messages mixed in with the ordinary output. Alternatively you could redirect stderr (descriptor 2) to a text file with a command like:
hcitool scan --info  --class 2>errors.txt | tee temp_file.txt

You would then need to inspect errors.txt after the command completes.
Vaclav_ 1-Mar-20 12:37pm    
Now BOTH of these EXAMPLES output to console.
BTW I have used HCI as an example, I guess I need to spell that out next time Gees.

cout <<"\t\t\t hciconfig -a 2>&1"<< endl;
system("hciconfig -a 2>&1");
cout <<"\t\t\t\hciconfig -a "<< endl;
system("hciconfig -a ");
Richard MacCutchan 1-Mar-20 12:57pm    
Exactly so. Is that a problem?
Vaclav_ 1-Mar-20 23:04pm    
YES it is a problem
shows that using "2>&1" is bogus IN THIS CASE.
Hence original question #1 answer is incomplete - FOR (MY) LEARNING purpose.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900