Last active
April 4, 2019 15:09
-
-
Save unakatsuo/faaa0177332f1b44376a969128798c13 to your computer and use it in GitHub Desktop.
Capture exit code from pipe command and stdout gets the original outputs.
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
#!/bin/bash | |
set -e | |
OUTPUTSCRIPT="test1" | |
test1(){ | |
timeout 3 grep 5 > /dev/null | |
echo "found 5" | |
} | |
output(){ | |
if [[ -z "$OUTPUTSCRIPT" ]]; then | |
cat | |
return 0 | |
fi | |
tee >($OUTPUTSCRIPT | sed -e "s/^/prefix: /") | |
wait $! | |
} | |
shopt -s lastpipe | |
shopt -so pipefail | |
for i in $(seq 1 10) | |
do | |
echo $i | |
done | output |
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
#!/bin/bash | |
set -e | |
OUTPUTSCRIPT="test1" | |
test1(){ | |
timeout 3 grep ok > /dev/null | |
echo "found 5" | |
} | |
output(){ | |
if [[ -z "$OUTPUTSCRIPT" ]]; then | |
cat | |
return 0 | |
fi | |
type $OUTPUTSCRIPT > /dev/null | |
tee /dev/tty | $OUTPUTSCRIPT | sed -e "s/^/${OUTPUTSCRIPT}: /" | |
} | |
shopt -s lastpipe | |
shopt -so pipefail | |
for i in $(seq 1 10) | |
do | |
echo $i | |
done | output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment