2

I have two calls that produce very different output:

Call one:

dmake -m _makefile_.m  1>> _results.out 2>> _results.out

Call two:

dmake -m _makefile_.m >2&1 >_results.out

dmake does a compile of sorts and the first call correctly inlines compile errors whereas the second one puts all the compile errors at the top. I was always of the opinion that both of these were equivalent. What exactly are the differences between these two calls? Is this because of buffering?

ojblass
  • 21,146
  • 22
  • 83
  • 132
  • 1
    Don't you mean `2>&1` rather than `>2&1`? Was that a typo in your question, or are you really using `>2&1`? – Keith Thompson Sep 26 '11 at 20:41
  • Yes, it's probably because of buffering. Output to stderr is typically unbuffered. [This question](http://stackoverflow.com/questions/3465619/how-to-make-output-of-any-shell-command-unbuffered) has some information about forcing stdout to be unbuffered, but it's ugly. – Keith Thompson Sep 26 '11 at 20:43
  • Really? What shell are you using? The `>2&1` syntax doesn't work for me in tcsh, bash, ksh, or zsh. Could that be the problem? Does the command create a file called `2`? – Keith Thompson Sep 26 '11 at 20:58
  • Or did you leave out a comma in your comment ("no, typo in the question")? – Keith Thompson Sep 26 '11 at 20:59
  • i don't see a file named 2 and the old way appeared to work... just with all the errors at the top. Maybe you are right. I didn't write call two and made the incorrect assumption that it had the correct syntax. I am using ksh by the way 2>&1 is what all my other scripts use. I am beginning to suspect you are correct Keith. – ojblass Sep 26 '11 at 21:31

1 Answers1

2

>2&1 is not the right syntax; it will redirect the output of the dmake command to a file called 2 (running it in background), then attempt to run a command called 1 with its output redirected to _results.out.

You want:

dmake -m _makefile_.m >_results.out 2>&1

Change > to >> if you want to append to the file.

I'm not 100% sure whether this will intersperse stdout and stderr the way you want.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631