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

Linux 在gnuplot中定义xrange时如何使用变量?,linux,bash,shell,gnuplot,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_t

我用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
#/bin/bash
#sar-P-script:将获取sar-P并绘制它的脚本
当前时间=$(日期+%t)
#将sar-P结果输入到文件中
sar-p111>sar-p1-11
新时间=$(日期+%t)
#删除第一行(您不需要它)
sed-i'/dennis/d'sar-P-1-1-11
#将空格设为逗号,然后压缩重复的逗号
tr-s'','new-sar-P-1-1-11
#将第一个字段剪切到新文件中(时间)
切割-d','-f 1新-sar-P-1-1-11>sar-11
#将最后一个字段剪切到新文件中(百分比)
切割-d','-f 8新-sar-P-1-1-11>sar-idle11
#创建x和y表
粘贴sar-times11 sar-idle11>sar-P-plots11
#删除我们不需要的任何内容(标题)
sed-i’/平均值/d’sar-P图11
sed-i'/idle/d'sar-P-plots11
./sar-P-gnuplot-script

你似乎把
bash
gnuplot
混在一起了。前两行看起来像
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

第一个错误

current_time=$(date +"%T")
new_time=$(date -d "10 seconds" +'%H:%M:%S')
gnuplot <<EOF
在运行sar-p之前,需要声明当前_时间,以及 运行sar-P后需要宣布新的_时间

第二个错误

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

gnuplot谢谢你的帮助,马克!假设我想在另一个脚本中执行这个。我可以在脚本中引用它吗?因为当我试着那样做的时候,它不起作用。我不确定我是否理解你想做什么。您可以将所有内容从
current_time=$(date+%T)
复制到
EOF
并粘贴到另一个
bash
脚本中,如果这是您的意思的话。当我按照您告诉我的方式运行它时,我得到了“第0行:警告:字符串中的时间格式不正确”。我有一个脚本从sar-P命令获取输出,然后我把它格式化成我想要的格式。在这个脚本中,我希望调用这个脚本,它将获取新的nice数据,并用gnuplot将其绘制成图形。非常有趣的是,所有内容的格式都是正确的,这里唯一的问题是使用变量而不是硬编码的时间。我想在这里使用这些变量的原因是,我不想在每次运行脚本时收集数据时硬编码特定的时间范围。您的原始行开头
new\u time=…
似乎是错误的。我已经在答案中更新了。谢谢你的编辑