Last active
September 21, 2017 08:12
-
-
Save pokle/d3e3e364b7850b7f3042dca1677378bb to your computer and use it in GitHub Desktop.
How to indent the output of a shell colorising stderr red
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Based on http://stackoverflow.com/questions/9112979/pipe-stdout-and-stderr-to-two-different-processes-in-shell-script | |
set -e | |
function indented() { | |
echo RUnNINg: $@ | |
(set -o pipefail; { "$@" 2>&3 | sed >&2 's/^/ | /'; } 3>&1 1>&2 | perl -pe 's/^(.*)$/\e[31m | $1\e[0m/') | |
} | |
function die() { echo ERROR: $@ > /dev/stderr; exit 1; } | |
echo Welcome to the indented running example. | |
echo | |
echo Here is a curl run: | |
indented curl -v http://example.com | |
echo | |
echo Here\'s a pipeline | |
indented bash -c 'echo -n abc | wc -c' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or even just sed!