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

时间:2015-05-19 09:51:11

标签: bash shell gnuplot

我想运行持久性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窗口刚出现并立即关闭。

我尝试了各种组合:

#!/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

进行调用

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

编辑基于Christoph评论:

这会打开并立即关闭窗口

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

这个甚至不打开窗口

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之后它有效,但我不知道为什么。

1 个答案:

答案 0 :(得分:0)

我在alias gnuplot='rlwrap -N -a -c -b"\"\"\\'\''\\'\''" gnuplot'之后定义了此别名unalias gnuplot