在gnuplot中定义xrange时如何使用变量?

时间:2018-07-13 15:47:26

标签: linux bash shell gnuplot

我用Bash写了一个脚本,我想用gnuplot绘制数据。当我用gnuplot调用此脚本时,错误消息是:

"sar-P-gnuplot-script", line 3: Column number or datablock line expected

似乎您在执行以下操作时无法定义任何变量:

gnuplot "*my_script*"

代码/脚本:

#!/bin/bash

current_time=$(date +"%T")
new_time=$(date -d "$current_time today + 10 seconds" +'%H:%M:%S')

set xdata time
set timefmt "%H:%M:%S"
set xrange ["$current_time":"$new_time"]
set format x "%H:%M:%S"
plot "sar-P-plots11" using 1:2
pause -1 "Hit any key to continue"

Bash脚本:

#!/bin/bash

# sar-P-script : script that will take sar-P and plot it

current_t=$(date +"%T")

# input the sar-P results into a file
sar -P 1 1 11 > sar-P-1-1-11

new_t=$(date +"%T")

# remove the first line (you don't need it)
sed -i '/dennis/d' sar-P-1-1-11

# makes the spaces commas, and then squeezes repeating commas
tr -s  ' ' ',' < sar-P-1-1-11 > new-sar-P-1-1-11

# cuts the first field into a new file (the times)
cut -d ',' -f 1 new-sar-P-1-1-11 > sar-times11

# cuts the last field into a new file (percentages)
cut -d ',' -f 8 new-sar-P-1-1-11 > sar-idle11

# creates an x and y table
paste sar-times11 sar-idle11 > sar-P-plots11

# cuts away anything that we don't need (headers)
sed -i '/Average/d' sar-P-plots11
sed -i '/idle/d' sar-P-plots11

./sar-P-gnuplot-script

2 个答案:

答案 0 :(得分:0)

您似乎混淆了bashgnuplot。前两行看起来像bash,其余看起来像gnuplot,所以您可能想要这样的东西:

#!/bin/bash

current_time=$(date +"%T")
new_time=$(date -d "10 seconds" +'%H:%M:%S')

# Start "gnuplot" passing in some "bash" variables
gnuplot <<EOF
set xdata time
set timefmt "%H:%M:%S"
set xrange ["$current_time":"$new_time"]
set format x "%H:%M:%S"
plot "sar-P-plots11" using 1:2
pause -1 "Hit any key to continue"
EOF

将脚本另存为plotit,然后使用以下命令将其可执行:

chmod +x plotit

然后运行:

./plotit

答案 1 :(得分:0)

First error:

current_time=$(date +"%T")
new_time=$(date -d "10 seconds" +'%H:%M:%S')

current_time needs to be declared before running sar-P, and new_time needs to be declared after running sar-P

Second Error

gnuplot <<EOF

needs to be

gnuplot -p <<EOF

This is to ensure that the plot will persist after the gnuplot function executes.