Bertrand Caron
Bertrand Caron

Reputation: 2657

Gnuplot - Command Substitution and Script Name

I'm using Gnuplot as my back-end plotter and I often use the following setup :

#Filename : my_plot.gnuplot
set terminal pdfcairo [my_options]
set output 'my_plot.pdf'
....

coupled with a Makefile :

%.pdf : %.gnuplot
    gnuplot $<

My question is simple : is there a command / way to refer to the name of the script inside the script (the equivalent of bash's $0) and set the output with a clever sprintf or equivalent ?

Upvotes: 0

Views: 286

Answers (2)

Daniel K.
Daniel K.

Reputation: 989

You can access the script name from gnuplot using the ARG0 variable.

To try this, just add show var ARG to your batch file and load it into gnuplot. This command will output all variables beginning with ARG.

To isolate only the filename (without the path), I'm using this piece of code:

start=1; while(1) { pos=strstrt(ARG0[start:], '\') ; if(pos==0) {break;}; start=start+pos; }; scriptname=ARG0[start:]

This works on Windows. For Unix/Mac you will have to adapt the path separator.

Upvotes: 0

Christoph
Christoph

Reputation: 48390

You cannot access the script name from gnuplot, but you can give a parameter when calling the script, which should work fine, especially when using Makefiles.

%.pdf: %.gnuplot
    gnuplot -e "scriptname='$<'" $<

In the plot file, this could be used with a command such as:

set output scriptname

Or from the command line:

gnuplot -e "scriptname='my_plot.gnuplot'" my_plot.gnuplot

Upvotes: 2

Related Questions