【问题标题】:How can you use variables when defining xrange in gnuplot?在 gnuplot 中定义 xrange 时如何使用变量?
【发布时间】:2020-07-15 21:55:19
【问题描述】:

我在 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

【问题讨论】:

  • 感谢编辑

标签: linux bash shell gnuplot


【解决方案1】:

您似乎混淆了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

【讨论】:

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

第一个错误

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

current_time 需要在运行 sar-P 之前声明,并且 new_time 需要在运行 sar-P 后声明

第二个错误

gnuplot <<EOF

需要

gnuplot -p <<EOF

这是为了确保在 gnuplot 函数执行后绘图将持续存在。

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-24
    • 2020-08-21
    • 2018-04-01
    相关资源
    最近更新 更多