3

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
alex
  • 1,319
  • 3
  • 16
  • 28
  • 1
    possible duplicate of [How to pass command line argument to gnuplot?](http://stackoverflow.com/questions/12328603/how-to-pass-command-line-argument-to-gnuplot) – Christoph Aug 12 '14 at 08:03

2 Answers2

1

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
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • 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 – alex Aug 12 '14 at 07:59
  • 1
    @alex: In that case, you should not be writing `${!fileName}` (which means "the value of the variable whose name is the value of `$fileName` -- for example, if `$fileName` is `Traffic3`, then `${!fileName}` means `$Traffic3`), but rather `$fileName`. I'll update my answer. – ruakh Aug 12 '14 at 15:17
0

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

gnuplot -e "filename=${fileName}" plotFile
konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • thanks for help. But it tell me: line 0: undefined variable: Traffic1 – alex Aug 12 '14 at 08:01
  • 1
    Because that should be `gnuplot -e "filename='${fileName}'" plotFile`. Please see [How to pass command line argument to gnuplot?](http://stackoverflow.com/questions/12328603/how-to-pass-command-line-argument-to-gnuplot). – Christoph Aug 12 '14 at 08:03
  • @Christoph thanks you christoph. god bless you you make my day. thanks. – alex Aug 12 '14 at 08:29