将Gnuplot变量传递给shell表达式

Jun*_*Jun 4 shell gnuplot

我试图在gnuplot中获取文件的第一个元素:

data = "file.dat"
x = `cat data | head -n 2 | tail -n 1 | awk '{print $1}'`
Run Code Online (Sandbox Code Playgroud)

但这不断给我以下错误:

no such file or directory
Run Code Online (Sandbox Code Playgroud)

我应该写一些像x =的东西 cat $data | head -n 2 | tail -n 1 | awk '{print $1}'

(带美元)

显然,这也是不正确的.

有任何想法吗?

Ten*_*gis 6

你需要使用一个

set macro
Run Code Online (Sandbox Code Playgroud)

然后使用符号@来获取变量的值data (@在Gnuplot中就像$在bash中)

所以这应该工作

x = `cat @data | head -n 2 | tail -n 1 | awk '{print $1}'`
Run Code Online (Sandbox Code Playgroud)

  • 正确,有效.但必须记住,宏扩展在反引号中起作用,但不在单引号和双引号内.为了说明这一点,请考虑以下示例:表达式``set macros; COL = "1"; 打印`echo @ col```output`1`,这是正确的,但是``set macros; COL = "1"; print`echo"@col"``退出并出错,因为宏没有扩展.但这只是一个旁注`:)`. (2认同)

Chr*_*oph 5

另一种可能性而非背景是使用该system功能.然后你可以构建任何字符串并将其作为shell表达式运行:

data = 'file.dat'
x = system("head -n 2 ".data." | tail -n 1 | awk '{print $1}'")
Run Code Online (Sandbox Code Playgroud)