Skip to content
Advertisement

Passing a Command as a Variable to another Program?

I’m creating an interactive script that allows me to plot specific parts of a data file using Gnuplot. These files contain multiple channels of instrumentation data and I’d like to plot specified channels together on the plot. At the moment I can only plot each channel independently with a script.

for ycolumn in $ycolumns; do 

    gnuplot -p << EOF
    set datafile separator ","
    set autoscale
    set grid
    set xlabel "Time"
    set ylabel "Data"
    set title "$graphTitle"
    plot "$FILE" using $xcolumn:$ycolumn lt rgb "red" with lines t " "
    EOF

This is the code that plots each channel independently. To plot them together I need the the plot line to look like this

plot "$FILE" using $xcolumn:$ycolumn1 lt with lines t " ","$FILE" using $xcolumn:$ycolumn2 lt with lines t " ", etc...

My question is how, if possible, could I have a for loop outside gnuplot that appends each column command (“$FILE” using $xcolumn:$ycolumn1 lt with lines t ” “) to a variable, and then that variable gets passed to the plot command inside Gnuplot?

I was thinking about using expect but I can’t figure out a way to implement that in the way I’m trying to automate this script.

Advertisement

Answer

Probably the most straightforward approach is to first assemble the entire plot command (as mentioned in the comments by ‘that other guy’) and then use it in the here document:

plotCmd=""
plotCmdDelim="plot"
for ycolumn in $ycolumns
do
    plotCmd="${plotCmd}${plotCmdDelim} '${FILE}' using $xcolumn:$ycolumn lt rgb 'red' with lines t ' '"
    plotCmdDelim=","
done

gnuplot -p << EOF
set datafile separator ","
set autoscale
set grid
set xlabel "Time"
set ylabel "Data"
set title "$graphTitle"
${plotCmd}
EOF
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement