4

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.

2

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, thanks a lot. This worked like a charm. – fhussain Oct 21 '12 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.