1

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 2

1

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"
3
  • I'm looking for where to file a bug report ... Commented Mar 31, 2014 at 14:33
  • Is there a reason you chose command date "$*" instead of command date "$@" ? Just curious, as I've found quoting issues tend to be lesser if you use $@
    – Wally
    Commented Mar 31, 2014 at 15:47
  • 1
    "$@" is when you want to pass the args to another program while preserving inner whitespace. "$*" is when you want to join all the args as a single string, which is what we're doing here. Commented Mar 31, 2014 at 15:53
1

It 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.

1
  • Yup, though my example was totally not properly escaped, I tried this version, and broke annotate-output. Have you tried it? If so, could you provide some details on the version of annotate-output you have used? Thanks Commented Apr 1, 2014 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .