简体   繁体   中英

Bash script : put commands on several lines with gnuplot

The following command is working fine when I type it in Bash (it generates histogram) :

$ cat input.dat | gnuplot -p -e 'set style data histograms; set style histogram cluster gap 1; set style fill solid border -1; set boxwidth 0.05; binwidth=0.1 ; bin(x,width)=width*floor(x/width) + binwidth/2.0; plot "-" using (bin($1, binwidth)):(1) smooth freq with boxes'

I would like to put this command in a file and on several lines. I tried for this :

#!/bin/bash

cat input.dat | gnuplot -p -e 'set style data histograms; set style histogram cluster gap 1; \                    
                bin(x,width)=width*floor(x/width) + binwidth/2.0; \
                plot "-" using (bin($1, binwidth)):(1) smooth freq with boxes'

but when I execute this shell script, I get the following error :

set style data histograms; set style histogram cluster gap 1; \
                                                              ^
line -3: invalid character \

The issue is that I divide gnuplot -p -e command on multiple lines but between simple quotes '... set style histogram cluster gap 1; \\ ... bin(x,width)=width*floor(x/width) + binwidth/2.0; \\ ... smooth freq with boxes' '... set style histogram cluster gap 1; \\ ... bin(x,width)=width*floor(x/width) + binwidth/2.0; \\ ... smooth freq with boxes'

This is not the same situation as when we divide linux command with backslash : in my case, I divide inside block of 2 simple quotes

Is there a way to circumvent this problem and split the first command above ?

Regards

If you switch the single quotes surrounding the expression with double quotes, and the double quotes surrounding the - with single quotes, you can escape the $1 and the script should work:

cat input.dat | gnuplot -p -e "set style data histograms; set style histogram cluster gap 1; \
            set style fill solid border -1; set boxwidth 0.05; binwidth=0.1 ; \
            bin(x,width)=width*floor(x/width) + binwidth/2.0; \
            plot '-' using (bin(\$1, binwidth)):(1) smooth freq with boxes"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM