alex
alex

Reputation: 1439

how to pass variable to -e on gnuplot?

I want to define gnuplot output which is a png file.how to define fileName on gnuplot.

    #This lines and also Traffic$j are define on my bash file.
    # Traffic$j is the name of file and that is valid. Traffic$j is on the loop
    # j is loop index
.
.

    fileName=Traffic$j
.
.

I try this:

gnuplot -e "filename=${!fileName}" plotFile

But I go this error:

line 0: constant expression required

I try ruakh's idea:

gnuplot -e "filename = '${!fileName}'" plotFile

But I gut this warning:

"plotFile", line 12: warning: Skipping data file with no valid points

line 12? look at my last line of the script.

How can I pass a variable to -e switch on gnuplot?

Update: My plotFile is this:

set terminal png size 720,450 enhanced font "H,11"
set output filename . '.png'

plot '../_numXY' using 2:3:(sprintf('%d', $1)) with labels offset 0,1 point pointtype 7 ps 2 lc rgb "green" notitle, \
filename using 1:2:($3-$1):($4-$2) with vectors linewidth 6 lc rgb "blue" notitle #line 12

Upvotes: 3

Views: 2896

Answers (2)

ruakh
ruakh

Reputation: 183602

The issue is not so much how to pass a variable, as how to quote a string. If, for example, $j is 3 and $Traffic3 is file.txt, then what you're passing to -e is filename=file.txt, when what you need to pass is something like filename = "file.txt" or filename = 'file.txt'. So:

gnuplot -e "filename = '${!fileName}'" plotFile

Edited to add: In a comment, you write:

thanks, $Traffic3 is not file.txt. or better to say I do not have $Traffic3 but I have Traffic3. Traffic3 is not parameter. It is the name of of file itself and not refer to another thing like file.txt

This means that you should not be writing ${!fileName}, but rather $fileName. The notation ${!fileName} means roughly, "O.K., so the value of $fileName is the name of another variable. Give me the value of that variable." So you just want:

gnuplot -e "filename = '$fileName'" plotFile

Upvotes: 1

konsolebox
konsolebox

Reputation: 75618

You probably didn't want to cast indirect variable expansion?

gnuplot -e "filename=${fileName}" plotFile

Upvotes: 0

Related Questions