Shell gnuplot if 块中的命令

Shell commands within if-block of gnuplot

我想在 Gnuplot 的 if 块中执行 shell 命令。我尝试了以下方法:

datatype = 'full'

if ( datatype eq 'full' ) {
    # Run shell command
    !echo 'full'
} else {
    # Run different shell command
    !echo 'not full'
}

但是,这给了我错误 "filename.plt" line xx: invalid command

仅供参考,我已经知道我可以使用 print 而不是 !echo 来做同样的事情。那不是重点。我想在 if 块中使用带有符号 ! 的 shell 命令。任何帮助将不胜感激。

@choroba 给出了正确的解决方案:使用 system("command") 函数而不是 !command.

至于为什么这是必要的,简而言之:

(1) 只有当 ! 运算符是 gnuplot 输入行上的第一个标记时,它才被解释为指示 shell 命令。否则它被解释为逻辑 NOT.

(2) Gnuplot 括号子句 { lots of commands } 在内部被视为单个长输入行。这是一个随时间变化的实现细节。最安全的一般准则是括号中的子句不应包含任何被记录为作用于或影响单行输入的语法。这包括 @ 宏替换、单行 if 语句,以及您发现的 ! 运算符。

(1+2) 因此,括号子句中的 ! 不被视为第一个标记,而是被解释为逻辑 NOT 运算符。