6

I need to use shell variables in my gnuplot commands, for which I'm using the here document style. I also need to use loops inside the gnuplot code. Both these things are working.

Now -- I want to use a gnuplot for loop's index to access a shell array variable. This is where I've been stuck all day.

I have something like this:

for ((i=0; i<=10; i++))
do
var[$i] = i*10
done

gnuplot<<EOF
do for [j=1:10]{
#need to access ${var[j]} somehow
val=sprintf("${var[%d]", j) ##doesn't work
}
EOF

Individual access like ${var[1]} works. I suspect this may need using backquotes and/or expr, but am not sure.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
fhussain
  • 61
  • 1
  • 3

1 Answers1

4

You're mixing up syntax here.

The shell will see ${var[%d] which looks a bit like a shell variable - but isn't. You should try

cat <<EOF
...
EOF

to debug and see exactly what is happening. What I think you want is something more like

for ((i=0; i<=10; i++))
do
var[$i] = i*10
done

gnuplot<<EOF
values="${var[*]}"
do for [j in values] {
#need to access ${var[j]} somehow
val=sprintf("%d", j) ##access part of the array directly
}
EOF
Julian
  • 899
  • 5
  • 5