在 unix 命令行命令中使用变量

发布于 2025-01-10 02:08:11 字数 590 浏览 1 评论 0原文

我正在尝试使用找到的步骤计算处理器的 CPU 负载 这里。我设法做到了这一点:

cat /proc/stat | head -n 1 | awk '{print ($5+$6, $2+$3+$4+$7+$8+$9)}' | awk '{print($1,$1+$2)}'

这给了我我需要的价值观。但是,我需要在一秒钟后计算相同的值,然后使用这两个结果来计算最终负载。这意味着,我需要做这样的事情:

cat /proc/stat | calculate something | awk '{print($1,$1+$2)}' ; sleep for a second; calculate again; use both of the results

有没有办法在第一个 awk 调用中保存变量 $1 和 $1+$2 ,以便我以后可以使用它们?我无法使用 bash 脚本,它需要在命令行中完成。

I am trying to calculate the CPU load of a processor using the steps found here. I managed to do this:

cat /proc/stat | head -n 1 | awk '{print ($5+$6, $2+$3+$4+$7+$8+$9)}' | awk '{print($1,$1+$2)}'

This gives me the values I need. However, I need to calculate the same values a second later and then use both of these results to calculate the final load. That means, that I need to do something like this:

cat /proc/stat | calculate something | awk '{print($1,$1+$2)}' ; sleep for a second; calculate again; use both of the results

Is there a way for me to save the variables $1 and $1+$2 in the first awk call, so that I can use them later? I cannot use a bash script, it needs to be done in a command line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗恋未遂 2025-01-17 02:08:11

您无需在现有命令的管道中拼凑对工具的单独调用。这:

cat /proc/stat | head -n 1 | awk '{print ($5+$6, $2+$3+$4+$7+$8+$9)}' | awk '{print($1,$1+$2)}'

可以重写为这样:

awk 'NR==1{x=$5+$6; print x, x+$2+$3+$4+$7+$8+$9; exit}' /proc/stat

听起来您可能想做的是这样的:

foo() { awk 'NR==1{x=$5+$6; print x, x+$2+$3+$4+$7+$8+$9; exit}' /proc/stat; }
{ foo; sleep 1; foo; } | awk 'NR==1{ p1=$1; p2=$2; next } { use p1, $1, p2, and $2 }'

如果您无法根据该信息做您想做的任何事情,那么编辑您的问题以提供更清晰的要求和一些简洁的信息、可测试、真正具有代表性的样本输入/输出。

You don't need to piece together separate calls to tools in a pipeline for your existing command. This:

cat /proc/stat | head -n 1 | awk '{print ($5+$6, $2+$3+$4+$7+$8+$9)}' | awk '{print($1,$1+$2)}'

can be rewritten as this:

awk 'NR==1{x=$5+$6; print x, x+$2+$3+$4+$7+$8+$9; exit}' /proc/stat

It sounds like what you might want to do is something like:

foo() { awk 'NR==1{x=$5+$6; print x, x+$2+$3+$4+$7+$8+$9; exit}' /proc/stat; }
{ foo; sleep 1; foo; } | awk 'NR==1{ p1=$1; p2=$2; next } { use p1, $1, p2, and $2 }'

If you can't do whatever it is you're trying to do given that information then edit your question to provide clearer requirements and some concise, testable, truly representative sample input/output.

凶凌 2025-01-17 02:08:11

一个简单的解决方案是首先创建完整的输入,然后使用 awk 处理它。我们首先定义 cpuLoad bash 函数以 1 秒的间隔捕获两个 CPU 负载:

$ cpuLoad () {
    head -n 1 /proc/stat
    sleep 1
    head -n 1 /proc/stat
  }
$ cpuLoad
cpu  8481582 17390 3183770 873720681 403491 0 708461 0 0 0
cpu  8481582 17390 3183774 873721078 403491 0 708461 0 0 0

然后我们可以提供一个 awk 脚本来实现精确的您引用的已接受答案的算法:

$ cpuLoad | awk '{
    Idle = $5+$6
    NonIdle = $2+$3+$4+$7+$8+$9
    Total = Idle + NonIdle
    totald = Total - PrevTotal
    idled = Idle - PrevIdle
    PrevTotal = Total
    PrevIdle = Idle
  }
  END {
    CPU_Percentage = 100.0 * (totald - idled) / totald
    print CPU_Percentage
  }'
4.987531

但我们可以简化很多:

$ cpuLoad | awk '{
    totald = $2+$3+$4+$5+$6+$7+$8+$9 - totald
    idled = $5+$6 - idled
  }
  END {
    print 100.0 * (totald - idled) / totald
  }'
0.501253

最后,我们可以将所有这些封装在 bash 函数中:

$ cpuLoad () {
    { head -n 1 /proc/stat; sleep 1; head -n 1 /proc/stat; } |
    awk '{t = $2+$3+$4+$5+$6+$7+$8+$9 - t; i = $5+$6 - i}
      END {print 100.0 * (t - i) / t}'
  }
$ cpuLoad
0.750000

A simple solution would be to create the complete input first and process it with awk next. We first define the cpuLoad bash function to capture two CPU loads with a 1 second interval:

$ cpuLoad () {
    head -n 1 /proc/stat
    sleep 1
    head -n 1 /proc/stat
  }
$ cpuLoad
cpu  8481582 17390 3183770 873720681 403491 0 708461 0 0 0
cpu  8481582 17390 3183774 873721078 403491 0 708461 0 0 0

Then we can feed an awk script that implements the exact algorithm of the accepted answer you cite:

$ cpuLoad | awk '{
    Idle = $5+$6
    NonIdle = $2+$3+$4+$7+$8+$9
    Total = Idle + NonIdle
    totald = Total - PrevTotal
    idled = Idle - PrevIdle
    PrevTotal = Total
    PrevIdle = Idle
  }
  END {
    CPU_Percentage = 100.0 * (totald - idled) / totald
    print CPU_Percentage
  }'
4.987531

But we can simplify a lot:

$ cpuLoad | awk '{
    totald = $2+$3+$4+$5+$6+$7+$8+$9 - totald
    idled = $5+$6 - idled
  }
  END {
    print 100.0 * (totald - idled) / totald
  }'
0.501253

And finally, we can encapsulate all this in the bash function:

$ cpuLoad () {
    { head -n 1 /proc/stat; sleep 1; head -n 1 /proc/stat; } |
    awk '{t = $2+$3+$4+$5+$6+$7+$8+$9 - t; i = $5+$6 - i}
      END {print 100.0 * (t - i) / t}'
  }
$ cpuLoad
0.750000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文