通过 -e 将变量传递给 gnuplot 会导致不持久 window

Passing variable to gnuplot via -e causes not persistent window

我想 运行 持久化 gnuplot 或将输出写入文件。此行为应受传递变量的影响。这是我的 test.plg:

#!/usr/bin/gnuplot -p
if (exists("file")) set term png; set output "test.png";
if (!exists("name")) exit;

set title name

plot sin(x)

如果我 运行 以这种方式编写脚本,它可以正常工作并将输出写入文件 test.png

gnuplot -e "name='GRAPH NAME GOES HERE'; file=''" test.plg

问题是当我不希望输出为图像而是持久输出时

gnuplot -e "name='GRAPH NAME GOES HERE'" test.plg

这次 gnuplot window 只是出现并立即关闭。

我尝试了这些的各种组合:

#!/usr/bin/gnuplot
gnuplot -e "name='GRAPH NAME GOES HERE'" test.plg

#!/usr/bin/gnuplot -p
gnuplot -p -e "name='GRAPH NAME GOES HERE'" test.plg

#!/usr/bin/gnuplot
gnuplot -p -e "name='GRAPH NAME GOES HERE'" test.plg

#!/usr/bin/gnuplot -p
gnuplot -e "name='GRAPH NAME GOES HERE'" test.plg

这样做的结果是 gnuplot 保持持久性的唯一方法是脚本以 #!/usr/bin/gnuplot -p 开始并直接用 ./test.plg 调用(而不是 gnuplot -e) 但是没有变量(本例中的name)不能被传递。

我发现的一种解决方法是不传递变量而是获取环境变量。这有点难看,需要修改以下代码:

#!/usr/bin/gnuplot -p

file="`echo $FILE`"
name="`echo $NAME`"

if (file ne "") set term png; set output "test.png";
if (name eq "") exit;

set title name
plot sin(x) 

然后通过以下命令控制脚本:

export FILE="T"
export FILE=""
export NAME="GRAPH NAME GOES HERE"

并通过 ./test.plg

直接调用

我想到的另一件事是首先打开保存图形的 window,然后以某种方式将 gnuplot 的输出加载到其中。但是在 ps -elf 输出下我找不到对应于打开图的过程。有人也可以解释一下吗?这真的是唯一可能的方法吗?

编辑 基于 Christoph 评论:

这打开并立即关闭window

gnuplot -p sine.plg
gnuplot -e '' -p sine.plg
gnuplot -p -e 'plot [-pi:pi] sin(x)'

这个竟然打不开window

gnuplot -e '' -p sine.plg

这个有效

gnuplot -p <sine.plg
echo 'plot [-pi:pi] sin(x)' |  gnuplot -p

EDIT2

我已经定义了这个别名alias gnuplot='rlwrap -N -a -c -b"\"\"\'\''\'\''" gnuplot' unalias gnuplot 之后它可以工作,但我不知道为什么。

我定义了这个别名 alias gnuplot='rlwrap -N -a -c -b"\"\"\'\''\'\''" gnuplot' unalias gnuplot 之后就可以了。