#1264 macro expansion in bash here document when in for loop

5.0
closed-fixed
nobody
4
2014-08-22
2013-07-22
Drew
No

gnuplot 4.7 from CVS 2013-07-21.

I am using a bash here document (the << EOF construct) to pass input to a gnuplot script, as shown in the MWE below. Call it with the name of an empty file as parameter.
The script produces the error message

more> do for [count=0:nsteps] {;  @DOPLOT
                              ^
      line 0: invalid character @

         line 0: Unexpected }

My current workaround is to use eval, however, it would be nice to understand what's going on. I might need to escape the at character @ but had no luck with google.
Interestingly, putting the macro invocation right after the open curly bracket { works.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
if [ -z "$1" ]
then
  echo "usage: $0 <input file>"
  exit
fi
inf="$1"

/usr/bin/env gnuplot << EOF

load "$inf"

set macros
DOPLOT="pause 0.25"
print "works:"
@DOPLOT
print "works:"
do for [count=0:1] { @DOPLOT
}
print "works:"
do for [count=0:1] {
  eval DOPLOT
}
print "doesn't work:"
do for [count=0:1] {
  @DOPLOT
}

EOF

Discussion

  • Ethan Merritt
    Ethan Merritt
    2013-07-22

    • status: open --> pending-fixed
     
  • Ethan Merritt
    Ethan Merritt
    2013-07-22

    Thank you for the bug report.
    I do not entirely understand why this problem is triggered by wrapping the commands in a shell script but not triggered when they are simply placed in a *.gp input file. Nevertheless, the failure mode is clear. In general input commands are assembled by reading 'enough' input to complete a command and then interpreting it. Macro substitution is done as the first step in interpretation. But in some cases, including the one you ran into, during interpretation the program discovers it has to extend the command by reading in additional lines of input. Unfortunately it was failing to re-check for macros in the new part of the extended command.

    Now fixed in CVS for 4.7
    The 4.6 source will need a similar fix before release of 4.6.4

     
  • Ethan Merritt
    Ethan Merritt
    2013-07-23

    • status: pending-fixed --> closed-fixed