1

I've been looking up some bash stuff today and a few snippets I've tried have included > > which seems to be the cause of the errors I'm receiving.

Example snippet:

command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)

Here's what I'm working with.

$ bash --help
GNU bash, version 3.2.48(1)-release-(x86_64-apple-darwin12)

EDIT
Here are the errors I'm gettting:

file.sh: line 14: syntax error near unexpected token `>'
file.sh: line 14: `command 2> >(while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done)'
Community
  • 1
  • 1
Curtis Blackwell
  • 3,130
  • 3
  • 27
  • 45
  • What are you trying to do exactly? What is command? And what are you trying to do with the '> >'? – Jared Burrows Nov 10 '12 at 03:17
  • Right, I know about `>` and `>>`. I don't really understand what `> >` is supposed to do, but I've seen it several times today in my googling, and it's been posted by people with high reputations and people have up-voted the code and whatnot. That specific line is supposed to colorize errors. You can click the [Example snippet](http://serverfault.com/questions/59262/bash-print-stderr-in-red-color) link for more info. – Curtis Blackwell Nov 10 '12 at 03:21
  • What errors are you getting? There's nothing wrong with that syntax. `2>` redirects stdout. `>(...)` is substituted with a name for a writable pipe into a subprocess, so it's a lot like `|` but with parentheses. – rici Nov 10 '12 at 03:42
  • 1
    By the way, the `>()` syntax is documented here in the bash manual: http://www.gnu.org/software/bash/manual/bashref.html#Process-Substitution . IMHO, reading through bashref.html would be a much better use of your time than googling random people's random comments about bash. But YMMV. – rici Nov 10 '12 at 03:45
  • @rici i added the errors. thanks for the link; I'll read up on that. – Curtis Blackwell Nov 10 '12 at 03:47
  • 2
    are you using `#!/bin/bash` or `#!/bin/sh` at the beginning of file.sh? The first is correct (probably); the second will either run a different shell program or cause `bash` to try to be compatible with `sh`. – rici Nov 10 '12 at 03:49
  • @rici: Re: "`2>` redirects stdout": Huh? Firstly -- it redirects standard *error* (file-descriptor `2`), not standard *output* (file-descriptor `1`). Secondly -- it only does that if it's followed by a file-name to redirect standard-output *to*. – ruakh Nov 10 '12 at 03:49
  • @ruakh; 1) yes, that was my typo. 2) it is followed by a filename because >() is substituted by a filename (by bash, not by sh) – rici Nov 10 '12 at 03:50
  • @rici: Re: #2: O.K., yes, that's true. – ruakh Nov 10 '12 at 03:51
  • I'm using almost exactly the same version of bash and don't get an error. When I put it into a script, same thing. What does the rest of your script say? How about the shebang line? – paulmelnikow Nov 10 '12 at 03:53

2 Answers2

1

According to the answers to this question, Bash 3.2.48 for Mac OS X has some limitations when it comes to process-substitution; it supports it in some cases but not others. Your case is apparently one where it doesn't.

Quoting from the best/most-helpful answer there:

The one from macports (4.2.37 — normally /opt/local/bin/bash if you have it installed) works fine. […] Perhaps you may want to use macports bash for this script.

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • It seems to work fine in the version I have, which is `3.2.48(1)-release (x86_64-apple-darwin11)`, almost but not quite the same. – paulmelnikow Nov 10 '12 at 04:30
0

The most naive way to solve your problem is the following:

while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done < <( { command > /dev/null ; } 2>&1 )

We're sending all stdout of command to /dev/null and then then redirecting the stderr to stdin.

Or the other way round:

{ command > /dev/null ; } 2>&1 | while read line; do echo -e "\e[01;31m$line\e[0m" >&2; done

With these methods you don't have to play too much with file descriptors!

Done.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104