if-statement - gnuplot 的 if block 中的 Shell 命令

标签 if-statement gnuplot

我想在 Gnuplot 的 if block 中执行 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 block 中使用带有符号 ! 的 shell 命令。任何帮助将不胜感激。

最佳答案

@choroba 给出了正确的解决方案:使用 system("command") 函数代替 !command

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

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

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

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

关于if-statement - gnuplot 的 if block 中的 Shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69196181/

相关文章:

javascript - 如何从第一个功能转到下一个功能?

c++ - 没有内部作用域的 if 语句?

Java if 语句

javascript - 三元运算符中的多项操作

gnuplot - 如何将 gnuplot 中的数据添加到现有文件而不覆盖该文件的内容

gnuplot - 使用 GNU plot 绘制文件中的数据列表

gnuplot - 在 Gnuplot 中绘制具有混合数据的多列文件

plot - 带有线条时的 gnuplot 突出显示点

gnuplot - gnuplot中的加号/减号?

r - 如何根据R中缺失数据的日期列计算数据框中多列的月平均值