Wakan Tanka
Wakan Tanka

Reputation: 8052

gnuplot script name OR bash $0 alternative for gnuplot

How to programmatically get the name of current gnuplot script? I know that I can call gnuplot script from bash and get it file name but I am wondering if it is possible from inside gnuplot. My goal is to make something like:

date=system("date +%F_%T | sed 's/:/-/g'")
my_name=$0 # THIS IS HOW TO DO IT IN BASH

set term png
set output my_name.date.".png"

I've tried:

my_name=system("cat /proc/$$/cmdline")

but it returned sh instead of script name

Upvotes: 1

Views: 566

Answers (2)

Christoph
Christoph

Reputation: 48420

Using gnuplot version 5 you have access to the file called with load via the variable ARG0

Consider the script test.gp which contains only

print ARG0

Now, calling this with

gnuplot -e "load 'test.gp'"

prints you test.gp on the screen. With earlier versions you don't have access to a similar variable (also not when using call). For earlier versions you must stick to one of the solutions given by @chw21

Upvotes: 2

chw21
chw21

Reputation: 8140

Not quite an answer to your question, but this might help with what you want to do:

You can leave my_name unset in the script, and set it either inside gnuplot, just before you load the script (where you need to know the script name anyway):

my_name=...
load(my_name)

or set it when you invoke gnuplot from the shell:

$ gnuplot -e "my_name=${FILE}" ${FILE}

A few more things:

date=system("date +%F_%T | sed 's/:/-/g'")

can be replaced with

date=system("date +%F_%H-%M-%S")

(which is shorter and doesn't need to be parsed through sed) or without any forking at all:

date=strftime("%F_%H-%M-%S",time(0.0))

Upvotes: 2

Related Questions