9

When running dos2unix on a file I get the following printed to the terminal

dos2unix: converting file <filename> to UNIX format ...

In my attempt to suppress the output by sending it to /dev/null I noticed that this is sent out on stderr instead of stdout as I'd expected (since it seems like a normal message, not an error). Is there a reason for this?

user1881282
  • 152
  • 2
  • 7
  • You'll find that unix is filled with historical artifacts, such as why `dd` requires arguments like `if=...`, and why `tar` requires positional arguments without the leading `-`. It is just the way it is... – Mark Lakata Jul 13 '14 at 07:28
  • 3
    In case you have not found it yet, you can suppress this output using the `--quiet` flag – ebo Jul 13 '14 at 07:28
  • Because it is bored. But in the past you could just pipe input into it and pipe stuff out. Errors where required to be placed in stderr – Ed Heal Jul 13 '14 at 07:36

5 Answers5

8

In Unix-like environments it is common to chain processes: The result of one program is used as input for another program. Mixing results with diagnostics would confuse the next processing stage. It would also hide the diagnostics from a potential user watching the terminal, where processing results piped to the next program don't show.

This is the reason for the separation of results and diagnostics in stdout and stderr. Diagnostics are not restricted to errors but should contain everything which is not a processing result which subsequent programs would expect.

With respect to the actual question: dos2unix is often used to transform files in-place but can also output to stdout (when called without a file name, it reads from stdin and outputs to stdout). stdout then can be redirected independently from stderr. Consider cat blados | dos2unix > blaunix. You would still see the diagnostics (which may contain error messages!), but the result of the processing will go to blaunix.

It's not so common to print diagnostics at all in case of success — probably a little nod to DOS users. It would be pretty bad if the processing result contained the informational message; for example, it would break a C file.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
6

There is no reason, but normally stderr is not just for error output. It is another stream that is often used for logging or informational messages. Since a log message is not output, it is not sent to stdout, which is for the results of the program.

The reason its printed on your terminal is a consequence of your shell, and not really controlled by the application.

Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • 2
    I bet that dos2unix used to be done manually using `tr -d '\r' < file > file2`, in which case stdin and stdout are already busy. – Mark Lakata Jul 13 '14 at 07:34
5
Try dos2unix -q <filename>

-q, --quiet Quiet mode. Suppress all warnings and messages. The return value is zero. Except when wrong command-line options are used.

kayle
  • 1,126
  • 13
  • 20
3

Simply because that's the way it was implemented...

If you'll check out the source-code you'll see:

...
if (!pFlag->Quiet)
    fprintf(stderr, _("dos2unix: converting file %s to file %s in UNIX format ...\n"), argv[ArgIdx-1], argv[ArgIdx]);

...
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 1
    And since it is [free software](https://en.wikipedia.org/wiki/Free_software) you can improve the source code to fits your needs (but you probably should publish your patches...) – Basile Starynkevitch Jul 13 '14 at 07:48
  • 1
    Although strictly spoken the question was not whether it was implemented (that much was obvious), but *why*. As a result, one could argue that your answer is less an answer than a confirmation of the state being. Nonetheless nice to look it up. – Peter - Reinstate Monica Jan 25 '15 at 08:00
1

I use this one-liner to redirect stderr to stdout, skip the resulting first irrelevant line, and send the rest back into stderr.

dos2unix thefile 2>&1|tail -n+2 1>&2

Rusty75
  • 477
  • 7
  • 19