Skip to content

Instantly share code, notes, and snippets.

@porn
Last active August 2, 2016 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porn/bd0d545820acdd2e7d6076c6158a4200 to your computer and use it in GitHub Desktop.
Save porn/bd0d545820acdd2e7d6076c6158a4200 to your computer and use it in GitHub Desktop.
prepend timestamp and custom text before stdout and stderr
#!/bin/bash
COMMAND=./test.sh
STDERR=./test.err
STDOUT=./test.out
# prepend timestamp followed by given string before each line of the output
IFS='' # preserve whitespaces
timestamp(){ while read line; do echo `date "+%Y-%m-%d %H:%M:%S"` $1 $line; done }
{ $COMMAND 2>&3 | timestamp "[$COMMAND]:" > $STDOUT; } \
3>&1 | timestamp "[$COMMAND] ERROR:" > $STDERR &
#!/bin/bash
sleep 1
echo "stderr sample" 1>&2
sleep 1
echo "stdout..."
echo " ...sample"
sleep 1
echo " stderr sample 2" 1>&2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment