【问题标题】:gnuplot internal column argument clashes with container bash script argumentsgnuplot 内部列参数与容器 bash 脚本参数冲突
【发布时间】:2020-08-03 07:05:35
【问题描述】:

我在 bash 脚本中有以下代码,用于准备输入数据以进行绘图。

代码在 gnuplot 解释器中运行时执行良好,但在 bash 脚本中,它用第二个 bash 参数替换了 $2,这意味着 ${outdir}/counts.txt 数据文件的列 2(输入文件没有不再处理情节)

如何编辑它以使 $1 和 $2 对 gnuplot 块保持私有?

  #!/bin/bash

  .. bash code

  gnuplot <<-EOFMarker
    set datafile separator ","
    set key autotitle columnhead
    set style line 1 \
        linecolor rgb '#0060ad' \
        linetype 1 linewidth 2 \
        pointtype 7 pointsize 1.5
    set yrange [*:100]
    set xlabel "mappability %"
    set ylabel "% above threshold"
    max(a,b)=a>b?a:b
    a=-99999999.
    set terminal pngcairo
    set output 'mappability_plot.png'
    plot '${outdir}/counts.txt' using 1:(a=max(a,$2),0/0) notitle, \
        '' using 1:(100*$2/a) notitle with linespoints linestyle 1
    EOFMarker

【问题讨论】:

    标签: bash gnuplot heredoc


    【解决方案1】:

    您可以通过使用反斜杠转义 $ 字符来防止 shell 在此处文档中扩展 $something

    gnuplot <<-EOFMarker
        [...]
        plot '${outdir}/counts.txt' using 1:(a=max(a,\$2),0/0) notitle, \
            '' using 1:(100*\$2/a) notitle with linespoints linestyle 1
        EOFMarker
    

    注意${outdir}没有转义,所以shell会继续正常替换。顺便说一句,您还需要转义您想要传递给程序而不被解释的任何反引号或反斜杠(转义)字符。

    顺便说一句,它不适用于此处,但您可以通过引用分隔符来关闭此处文档中的 所有 替换:

    gnuplot <<-'EOFMarker'
        $things `that` $won't \be $expanded
    EOFMarker
    

    在这里不起作用的原因是它也会阻止 shell 扩展 ${outdir},我想这不是你想要的。

    【讨论】:

    • 我尝试转义 2 美元,但没有成功。相比之下,使用 column(2) 就可以了。
    【解决方案2】:

    试试column(1)column(2),而不是$1$2...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多