There are two things going wrong here. One of them is that I screwed up and gave you a program that works with GNU sed but fails with BSD sed (Are you by any chance using OS X?). I think you can fix that by adding a $ immediately before each opening single quote, so that the escapes are interpreted by the shell instead of by sed:
$ cat testdrive_afni.bash
#!/bin/bash
afni -yesplugouts "$@" > >(sed $'s/.*/\x1b\[31m&\x1b\[0m/') \
2> >(sed $'s/.*/\x1b\[1;31m&\x1b\[0m/' 1>&2) &
plugout_drive -v 2> >(sed $'s/.*/\x1b\[1m&\x1b\[0m/' 1>&2)
("<ESC> [
n m" is a terminal escape code, where
n is some comma-separated sequence of numbers describing the attributes you want the following text to have. 1 means bold, 31 means red, and 0 means return to normal. The sed commands just tack appropriate terminal escapes onto the beginning and end of each line coming from either program; the problem was that GNU sed interprets \x1b as <ESC>, while BSD sed interprets it as x1b. The fix here makes it so that by the time the command gets to sed, it's already an <ESC> character.)
The other problem is that for some reason, AFNI doesn't flush the stream (i.e., actually write the output that it has queued up for writing) when it executes a GET_DICOM_XYZ command, so that output won't be written until some other command (like GETENV or closing the stream on QUIT) causes the stream to flush. Also, because this solution pipes output through sed, AFNI's output is line-buffered (i.e., save up output until you can send an entire line into the pipe) instead of unbuffered (i.e., print it as soon as you have it, as it would do if the output were directly to the terminal), which is why you get the cruft about NLfit & NLerr on the same line-- AFNI wrote that a while ago, but it didn't get printed until the line was finished. You can see this effect by running something like "3dClustSim -options -go -here | cat"; the progress bar won't appear until the program is finished.
I think the easiest way to deal with this is to include a dummy "GETENV bogus" at the beginning and end of your script, so that the partial line will be flushed. So you could do something like this:
afni -yesplugouts | grep 'RAI xyz' > coords.txt&
{
echo GETENV bogus
while :; do
read -n1 -r -p $'\nPress "q" to quit or any other key to dump coordinates' 1>&2
if [ "$REPLY" == q ]; then
break
fi
echo GET_DICOM_XYZ
done
echo GETENV bogus
} | plugout_drive &>/dev/null
And then click around in the viewer between keypresses in the terminal.
HTH,
ijs
Edited 1 time(s). Last edit at 06/02/2015 02:52PM by Isaac Schwabacher.