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' |
Or even just sed!
ls -l | sed 's/^/ --| /'
--| total 784
--| -rw-r--r-- 1 tpokle SEEK\Domain Users 2377 21 Sep 17:04 Makefile
--| -rw-r--r-- 1 tpokle SEEK\Domain Users 349 20 Sep 14:55 Makefile.development.cf
--| -rw-r--r-- 1 tpokle SEEK\Domain Users 681 20 Sep 14:55 Makefile.production.cf
--| -rw-r--r-- 1 tpokle SEEK\Domain Users 9181 20 Sep 14:55 README.md
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An alternative would be to use the
nl
command: