Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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