2024-06-25 23:17:41 发布
网友
我想使用gnuplot从数据文件中绘制图形,比如foo.data。目前,我在命令文件中硬编码了数据文件名,比如foo.plt,并运行命令gnuplot foo.plg来绘制数据。但是,我想将数据文件名作为命令参数传递,例如运行commandgnuplot foo.plg foo.data。如何解析gnuplot脚本文件中的命令行参数?谢谢。
gnuplot foo.plg
gnuplot foo.plg foo.data
您可以使用标记-c将参数传递给5.0版以后的gnuplot脚本。这些参数通过变量ARG0到ARG9、ARG0作为脚本和ARG1到ARG9字符串变量访问。参数的数目由ARGC给出。
-c
ARG0
ARG9
ARG1
ARGC
例如,下面的脚本(“script.gp”)
#!/usr/local/bin/gnuplot --persist THIRD=ARG3 print "script name : ", ARG0 print "first argument : ", ARG1 print "third argument : ", THIRD print "number of arguments: ", ARGC
可以称为:
$ gnuplot -c script.gp one two three four five script name : script.gp first argument : one third argument : three number of arguments: 5
或者在gnuplot内
gnuplot> call 'script.gp' one two three four five script name : script.gp first argument : one third argument : three number of arguments: 5
在gnuplot 4.6.6和更早的版本中,存在一个call机制,其语法不同(现在已弃用)。参数通过$#,$0,…,$9访问。例如,上面的相同脚本看起来像:
call
$#
$0
$9
#!/usr/bin/gnuplot --persist THIRD="$2" print "first argument : ", "$0" print "second argument : ", "$1" print "third argument : ", THIRD print "number of arguments: ", "$#"
它在gnuplot中被称为(记住,version<;4.6.6)
gnuplot> call 'script4.gp' one two three four five first argument : one second argument : two third argument : three number of arguments: 5
注意,脚本名没有变量,所以$0是第一个参数,变量在引号中调用。不能直接从命令行使用它,只能通过@con fu se建议的技巧。
您还可以按照建议通过环境传入信息here。这里重复了Ismail Amin的例子:
在外壳中:
export name=plot_data_file
在Gnuplot脚本中:
#! /usr/bin/gnuplot name=system("echo $name") set title name plot name using ($16 * 8):20 with linespoints notitle pause -1
您可以通过开关-e输入变量
-e
$ gnuplot -e "filename='foo.data'" foo.plg
在foo.plg中,可以使用该变量
$ cat foo.plg plot filename pause -1
要使“foo.plg”更通用一些,请使用条件:
if (!exists("filename")) filename='default.dat' plot filename pause -1
注意-e必须在文件名之前,否则文件将在-e语句之前运行。特别是,使用./foo.plg -e ...CLI参数运行shebang gnuplot#!/usr/bin/env gnuplot将忽略使用提供的参数。
./foo.plg -e ...
#!/usr/bin/env gnuplot
您可以使用标记
-c
将参数传递给5.0版以后的gnuplot脚本。这些参数通过变量ARG0
到ARG9
、ARG0
作为脚本和ARG1
到ARG9
字符串变量访问。参数的数目由ARGC
给出。例如,下面的脚本(“script.gp”)
可以称为:
或者在gnuplot内
在gnuplot 4.6.6和更早的版本中,存在一个
call
机制,其语法不同(现在已弃用)。参数通过$#
,$0
,…,$9
访问。例如,上面的相同脚本看起来像:它在gnuplot中被称为(记住,version<;4.6.6)
注意,脚本名没有变量,所以
$0
是第一个参数,变量在引号中调用。不能直接从命令行使用它,只能通过@con fu se建议的技巧。您还可以按照建议通过环境传入信息here。这里重复了Ismail Amin的例子:
在外壳中:
在Gnuplot脚本中:
您可以通过开关
-e
输入变量在foo.plg中,可以使用该变量
要使“foo.plg”更通用一些,请使用条件:
注意
-e
必须在文件名之前,否则文件将在-e
语句之前运行。特别是,使用./foo.plg -e ...
CLI参数运行shebang gnuplot#!/usr/bin/env gnuplot
将忽略使用提供的参数。相关问题 更多 >
编程相关推荐