1
votes

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

2
The sh you got is the command line of the system call, not your script. Also, this question seems to have nothing to do with bash. - 4ae1e1
Bash was used just as example language where it is possible to demonstrate what I'm trying to achieve. - Wakan Tanka
That doesn't make it a bash question. - 4ae1e1

2 Answers

2
votes

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))
2
votes

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