Hello Athul,
On Sat, Aug 29, 2020 at 12:29:37AM +0530, Athul R T wrote:
echo "hello" | tee -a log.txt
can write the output to console as well as file.
How can I write the result of a make target command like this as the above method works only on console. This is the format of execution of make.
It should work in shell script too. Scripts and commands communicate with us using 'streams'. There are 3 standard streams:
1. Standard input (stdin): The input to the command 2. Standard output (stdout): The output goes to this stream 3. Standard error (stderr): Error and diagnostic output of the command
The command output you normally see on a terminal is actually what the command writes to stdout and stderr.
The following syntax: cmd1 | cmd2
is called 'piping'. The stdout of cmd1 is sent (piped) to stdin of cmd2. Sometimes, you may want to pipe both stdout and stderr to another command. This is true for your case. You want both output and error message from shell script to be saved. The syntax is:
cmd1 2>&1 | cmd2
This means that the stderr (stream 2) is redirected to stdout (stream 1). After that, stdout is piped to cmd2.
tee command sends data stream from its stdin to a file as well as the stdout (terminal). So this should work inside the shell script:
make target 2>&1 | tee build.log
Regards, Gokul Das