redirecting complete make output to a file - bash

I want to redirect complete make output to a file.
I tried redirecting the stdout and stderr with the following command:
make >aks_file.txt 2>&1 &
But that is not redirecting the EXACT complete output which is otherwise generated by issuing just make (some lines are missing)
Am I missing something?

Some programs (either make or what make invokes) can detect whether their output streams are attached to the console or file and change their behavior (what they output) accordingly. To be more specific, more details on what output exactly is missing and by what program it is being generated when ran without redirection is needed.

Related

Bash script - run process & send to background if good, or else

I need to start up a Golang web server and leave it running in the background from a bash script. If the script in question in syntactically correct (as it will be most of the time) this is simply a matter of issuing a
go run /path/to/index.go &
However, I have to allow for the possibility that index.go is somehow erroneous. I should explain that in Golang this for something as "trival" as importing a module that you then fail to use. In this case the go run /path/to/index.go bit will return an error message. In the terminal this would be something along the lines of
index.go:4:10: expected...
What I need to be able to do is to somehow change that command above so I can funnel any error messages into a file for examination at a later stage. I tried variants on go run /path/to/index.go >> errors.txt with the terminating & in different positions but to no avail.
I suspect that there is a bash way to do this by altering the priority of evaluation of the command via some judiciously used braces/brackets etc. However, that is way beyond my bash capabilities. I would be most obliged to anyone who might be able to help.
Update
A few minutes later... After a few more experiments I have found that this works
go run /path/to/index.go &> errors.txt &
Quite apart from the fact that I don't in fact understand why it works there remains the issue that it produces a 0 byte errors.txt file when the command goes to completion without Golang throwing up any error messages. Can someone shed light on what is going on and how it might be improved?
Taken from man bash.
Redirecting Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be redirected to the file whose name is the expansion of word.
There are two formats for redirecting standard output and standard error:
&>word
and
>&word
Of the two forms, the first is preferred. This is semantically equivalent to
>word 2>&1
Appending Standard Output and Standard Error
This construct allows both the standard output (file descriptor 1) and the standard error output (file descriptor 2) to be appended to the file whose name is the expansion of word.
The format for appending standard output and standard error is:
&>>word
This is semantically equivalent to
>>word 2>&1
Narūnas K's answer covers why the &> redirection works.
The reason why the file is created anyway is because the shell creates the file before it even runs the command in question.
You can see this by trying no-such-command > file.out and seeing that even though the shell errors because no-such-command doesn't exist the file gets created (using &> on that test will get the shell's error in the file).
This is why you can't do things like sed 'pattern' file > file to edit a file in place.

How can i print output of a command to a file in Batch Script?

I am trying to call a command in batch file using "call" method and whatever is the output of that command, I want to write in a file.
I went through from this link but cannot find the answer.
I am using this command
call %confPath% GetIniString %datFile% Keyname name >%newFile% >&1
but it creates a empty file always. How can i write the output of above command in the file?
Thanks in advance.
>%newFile% redirects the standard output to a file. in >&1, the 1 stands for standard output, and if no stream is specified, standard output is the default, so >&1 redirects on itself, although it was already redirected with the first command. So, this is illegal and shouldn't produce a file at all. In my tests, this just aborts with an errormessage.
The usual idiom 2>&1, OTOH, redirects stream 2, which is standard ERROR, to standard output, which ensures that both output and error messages end up in the file.

Duplicating stderr compilation output into separate file

I have a project, which builds by using make, and I want to add possibility
to analyze overall state of warning messages.
My idea is change make rules in order to duplicate stderr compilation output into separate file during full rebuild. Means each time make all will be done, all output will be printed in console and in addition stderr output will be duplicating into separate file.
This warning report file will be added into repository, so that I will have possibility to compare warnings existing in repository and local warnings.
The question is how to DUPLICATE (not redirect) stderr output into separate file? Means how I should change all target in Makefile?
I know how to redirect stderr output (make all 2>warning_report.txt), but it is not
what I need. Warning messages should be both in main console output and in warning file.
I use Windows 7 as work environment but I had no any deal with Windows command line or batch files before.
Thanks in advance.
Edited:
In my case final solution looks like below:
make all 3>&1 1>&2- 2>&3- | tee.bat warning_report.txt
In this case script tee.bat, which is written in JScript for Windows, I took from link specified by PA (thanks).
What about swapping, I took it from here and here.
I don't know about windows but you can do it using tee command in Linux. tee is used to redirect STDOUT to file as well as console so you can take its advantage and check if you can solve your problem.
make all 2>&1 1>stdout.log | tee stderr.log
redirect STDERR to STDOUT, redirect STDOUT to stdout.log and all the STDERR is copied to stderr.log ans echoed on the console as well.
But the solution is not complete yet. The STDOUT is not printed on the console but only copied to the stdout.log. Try playing around the commands you will get the solution .
I just re-read your question and decided I would try to answer it.
Here's a snippet made to export stderr and display it.
#echo off
if exist stderr.error del stderr.error
this_is_not_a_command 2>stderr.error
if exist stderr.error type stderr.error & del stderr.error
This would export stderr to a file and then display the contents of that file.
Hope that helps.

Hopw to stop the window from exiting during silent installation using batch file?

I wrote a batch file which does silent installation which is working fine. But how do I read the error messages if any appear? Can I write all the error/success messages to a log file? Also is there any command to stop the window from exiting?
There's the PAUSE command, which does nothing else then printing a message (Press any key to continue . . .) and waiting till a key is pressed. That would allow you to read any messages before the window goes. Just add the command to the batch file before the end of the script and/or at other position where you need it.
You could also try redirecting messages to a file. Typically console messages are redirected by adding >filename or 1>filename to the command line.
However, that would only redirect stdout messages, while there might also be stderr ones. Particularly, error messages are often printed to stderr, although that is not a rule and third-party programs may not follow that convention. Anyway, stderr messages would need to be redirected with 2>filename put on the command line.
To redirect both and make sure they go to the same file, use 1>filename 2>&1 on the command line.
You can add the redirection either to specific commands in the script or to the batch file in general. If you redirect specific commands of which there's more than one and you want the results to be logged in the same file, you'll need to use >> instead of > on all or at least all but the first command. That's because > would rewrite the output file if it existed and >> would append to it.

Redirection Doesn't Work

I want to put my program's output into a file. I keyed in the following :
./prog > log 2>&1
But there is nothing in the file "log". I am using the Ubuntu 11.10 and the default shell is bash.
Anybody know the cause of this AND how I can debug this?
There are many possible causes:
The program reads the input from log file while you try to redirect into it with truncation (see Why doesn't "sort file1 > file1" work?)
The output is buffered so that you don't see data in the file until the output buffer is flushed. You can manually call fflush or output std::flush if using C++ I/O stream etc.
The program is smart enough and disables output if the output stream is not a terminal.
You look at the wrong file (i.e. in another directory).
You try to dump file's contents incorrectly.
Your program outputs '\0' as the first character so the output appears to be empty, even though there is some data.
Name your own.
Your best bet is to run this application under a debugger (like gdb) or use strace or ptrace (or both) and see what the program is doing. I mean, really, output redirection works for the last like 40 years, so the problem must be somewhere else.

Resources