I try to use annotate-output (part of devscripts package), to prefix outputs of a script with date and some extra info. First parameter can be a format string passed to date. I want to include a space in this format string, however, being a bash newbie I have no idea, how to escape the space to get it sent to date properly. Tried a bunch of options, but looks like no matter what I provide, sys just eats it...
Source of annotate-output can be found HERE.
user@host:/home/user$ annotate-output '+myscript: %H:%M:%S' echo "A" date: extra operand `%H:%M:%S' Try `date --help' for more information. I: Started echo A date: extra operand `%H:%M:%S' Try `date --help' for more information. O: A date: extra operand `%H:%M:%S' Try `date --help' for more information. I: Finished with exitcode 0 Expected output:
myscript: 14:04:16 I: Started echo A myscript: 14:04:16 O: A myscript: 14:04:16 I: Finished with exitcode 0 2 Answers
Unfortunately that script has some quoting issues. It will work if you add this function to the code:
date () { command date "$*" } Actually you don't need to alter the source. Do this: define the "date" function and export it:
$ date() { command date "$*"; } $ export -f date $ annotate-output "+foo %T" bash -c "echo stdout; echo stderr >&2" foo 10:38:30 I: Started bash -c echo stdout; echo stderr >&2 foo 10:38:30 E: stderr foo 10:38:30 O: stdout foo 10:38:30 I: Finished with exitcode 0 There might be problems if the thing you're annotating also calls date.
If you care to fix annotate-output on your system, apply this diff
$ diff /usr/bin/annotate-output bin/annotate-output 28c28 < echo "`date ${FMT}` $1: $line" --- > printf "%s %s: %s\n" "$(date "$FMT")" "$1" "$line" 78c78 < echo "`date ${FMT}` I: Started $@" --- > addtime I <<< "Started $*" 83c83 < echo "`date ${FMT}` I: Finished with exitcode $EXIT" --- > addtime I <<< "Finished with exitcode $EXIT" 3It will work if you quote parameter properly:
annotate-output '+"myscript: %H:%M:%S"' echo "A" date command's format argument starts with +. That's right. But in order to pass space into it you should wrap all chars after + with quotes. Otherwise it will accept symbols which are following the whitespace as second command line argument.