Search code examples
bashgnuplotheredoc

gnuplot internal column argument clashes with container bash script arguments


I have the following code within a bash script which prepares the input data to plotting.

The code executes fine when run in a gnuplot interpreter but in the bash script it replaces $2 which is meant to represent columns 2 of the ${outdir}/counts.txt data file by the second bash arguments (input file having nothing to do anymore with the plot)

How can i edit this so that $1 and $2 remain private to the gnuplot block?

  #!/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

Solution

  • Instead of $1 and $2, try column(1) and column(2)...