Christopher Sun
Christopher Sun

Reputation: 3

Unknown syntax in Gnuplot?

I am learning pass BASH list to Gnuplot without generate an intermediate file. The answer I found was very useful (by Christoph at Set parameters of Gnuplot from array in bash script). And the code is below.

I am confused about two symbols. One is " in values="${params[]}*. The other is . in eval('set '.val). I did not find the syntax in the manual. Could you tell me what they are for?

### Code
#!/bin/bash
params[0]='grid'
params[1]='xrange[0:10]'

gnuplot -persist << EOF
values="${params[*]}
do for [val in values] {
    eval('set '.val)
}
plot x
EOF
####

Upvotes: 0

Views: 90

Answers (1)

that other guy
that other guy

Reputation: 123410

There here document is constructed from shell variables. ${params[*]} is bash code for "all elements in the array params concatenated into a string". It's not all gnuplot code.

Replace gnuplot -persist with cat to see what gnuplot sees:

#!/bin/bash                                                                  
params[0]='grid'                                                             
params[1]='xrange[0:10]'                                                     

cat << EOF                                                                   
values="${params[*]}                                                         
do for [val in values] {                                                     
    eval('set '.val)                                                         
}                                                                            
plot x                                                                       
EOF                                                                          

resulting in:

values="grid xrange[0:10]
do for [val in values] {
    eval('set '.val)
}
plot x

values="grid xrange[0:10] is a variable assignment:

gnuplot> values="grid xrange[0:10]
gnuplot> print values
grid xrange[0:10]

. is string concatenation:

gnuplot> print "foo" . "bar"
foobar

So the result is that set grid and set xrange[0:10] are evaluated by gnuplot as if you had typed them in manually.

Upvotes: 1

Related Questions