Created
September 24, 2011 23:12
-
-
Save travisbhartwell/1239973 to your computer and use it in GitHub Desktop.
Yay Fun Hack
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 | |
# Don't allow unset variables | |
set -o nounset | |
# Exit if any command gives an error | |
set -o errexit | |
# Constants | |
LOG_DIR=$HOME/tmp | |
LOG_FILE="${LOG_DIR}"/test.log | |
LOG_PIPE=/tmp/${0##*/}.pipe | |
setup () { | |
if [ ! -e "${LOG_PIPE}" ]; then | |
mkfifo "${LOG_PIPE}" | |
fi | |
if [ -e "${LOG_FILE}" ]; then | |
rm "${LOG_FILE}" | |
fi | |
# Redirect stdout and stderr through tee to simultaneously | |
# log and display | |
exec 3>&1 4>&2 | |
tee "${LOG_FILE}" < "${LOG_PIPE}" >&3 & | |
tee_pid=$! | |
exec > >( | |
while read line; do | |
echo $(date +"%Y/%m/%d %H:%M:%S")"| ${line}" | |
done > "${LOG_PIPE}" 2>&1 | |
) | |
trap cleanup EXIT HUP INT QUIT TERM | |
} | |
cleanup () { | |
# Close temporary file descriptors for stdout and stderr | |
exec 1>&3 3>&- 2>&4 4>&- | |
wait $tee_pid | |
rm -f "${LOG_PIPE}" | |
} | |
dostuff () { | |
for i in $(seq 1 100); do | |
local count=$[ ( $RANDOM % 55 ) + 1 ] | |
for j in $(seq 1 $count); do | |
echo -n "${j} " | |
done | |
echo "" | |
done | |
} | |
setup | |
dostuff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment