Bash脚本:使用gnuplot将命令放在多行上

时间:2017-10-26 20:49:01

标签: linux bash shell

当我在Bash中键入它时,以下命令正常工作(它生成直方图):

$ 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'

我想将此命令放在一个文件和几行中。我试过这个:

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

但是当我执行这个shell脚本时,我收到以下错误:

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

问题在于我将gnuplot -p -e命令分为多行而是简单引号'... set style histogram cluster gap 1; \ ... bin(x,width)=width*floor(x/width) + binwidth/2.0; \ ... smooth freq with boxes'

这与我们用反斜杠划分linux命令时的情况不同:在我的情况下,我在2个简单引号的块内划分

有没有办法绕过这个问题并拆分上面的第一个命令?

此致

1 个答案:

答案 0 :(得分:0)

如果使用双引号切换表达式周围的单引号,并使用单引号切换 - 的双引号,则可以转义$ 1并且脚本应该有效:

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"