#!/usr/bin/ksh

# simple script to add time stamps to the output of a command
#
# Author: Bernd Schemmer, Bernd.Schemmer@de.ibm.com
#
# Usage: addtimestamp [command_to_execute] [parameter_for_the_command]
#
# History
#    09.05.2008 /bs v1.0.0
#      initial version
#
## --------------------------------------
## PrintWithTimestamp
##
## print the output of a command to STDOUT with a timestamp
##
## usage: PrintWithTimestamp command_to_execute [parameter]
##
## returns: 0
##
## Note: This function does not write to the log file!
##
## Source: Bernd Fingers blog:
##         http://blogs.sun.com/blogfinger/entry/prepend_command_output_lines_with
##
function PrintWithTimestamp {
  typeset COMMAND="$*"
  echo "Executing \"${COMMAND}\" ..."

  sh -c "${COMMAND} | nawk '{\"date \\\"+%d.%m.%Y %H:%M:%S\\\" #\"|getline date;
  close(\"date \\\"+%d.%m.%Y %H:%M:%S\\\" #\");
  printf (\"%s %s\n\", date, \$0)}'"
}

if [ $# -eq 0 -o "$1"x = "-h"x ] ; then
  echo ""
  echo "Usage: $0  command_to_execute [parameter]"
  echo ""
  echo "Example $0 iostat 5 30"
  echo ""
  exit 5
fi

PrintWithTimestamp $*


