我有一个bash脚本,通过gnuplot生成图形。只要输入文件名不包含空格,一切正常。
以下是我的脚本:
INPUTFILES=("data1.txt" "data2 with spaces.txt" "data3.txt")
...
#MAXROWS is set earlier, not relevant.
for LINE in $( seq 0 $(( MAXROWS - 1 )) );do
gnuplot << EOF
reset
set terminal png
set output "out/graf_${LINE}.png"
filenames="${INPUTFILES[@]}"
set multiplot
plot for [file in filenames] file every ::0::${LINE} using 1:2 with line title "graf_${LINE}"
unset multiplot
EOF
done
这段代码本身是可行的,但只有在输入文件名中没有空格的情况下才能正常工作。
按照 gnuplot 的示例进行评估:
1 iteration: file=data1.txt - CORRECT
2 iteration: file=data2 - INCORRECT
3 iteration: file=with - INCORRECT
4 iteration: file=spaces.txt - INCORRECT
seq
。使用for ((line=0; line < maxrows; line++)); do
。不要使用全大写的变量名。除非文件名不含有空格,否则你的问题表述有误。在heredoc中没有引号移除。"${INPUTFILES[@]}"会扩展为由双引号包围的以空格分隔的元素列表。如果这在gnuplot中用于指定文件列表有效,那么你需要想办法转义空格。 - ormaaj