1

我无法在 shell 脚本 (/bin/sh) 中将 CPU[3] 捕获到 IDLE 变量

代码片段如下:-

while true; do
      CPU=$(sed -n 's/^cpu\s//p' /proc/stat)
      **IDLE=$CPU[3]**                                   # Just the idle CPU time.
      for VALUE in ${CPU} ;
      do
       TOTAL=$((TOTAL+VALUE))
       printf "%s \n\n" $VALUE
       sleep 1
       printf "%s \n" $IDLE
      done
      sleep 0.1
 done
4

1 回答 1

0

您可以尝试使用以下脚本从 sed 命令中提取特定的字符串元素。

#!/bin/sh

CPU=$(sed -n 's/^cpu\s//p' /proc/stat) 
IDLE=$((sed -n 's/^cpu\s//p' /proc/stat)| (cut -d' ' -f5))
printf "IDLE Check %s \n" $IDLE
printf "CPU: %s \n" $CPU

但是我看到提取的值是递增的,我会研究一下并在一段时间内更新,祝你好运!!!

我得到如下输出:

IDLE Check 286857 /*value is incremented by 2 , i will update in while on this */
CPU: 32898 
CPU: 6 
CPU: 8128 
CPU: 286855  /* This value is to be extracted as example */
CPU: 27052 
CPU: 0 
CPU: 462 
CPU: 0 
CPU: 0 
CPU: 0 
于 2019-12-07T12:26:44.470 回答