Linux 命令top-b-n1 | grep Cpu总是返回相同的值

Linux 命令top-b-n1 | grep Cpu总是返回相同的值,linux,shell,grep,Linux,Shell,Grep,当我运行命令top-b-n1 | grep Cpu时,它总是返回相同的值: Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st 但是当我删除-n1部分时,结果以 Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st 随后的值就不同了 有什么原因吗?如何获取不同的值?检查您的主页: The top comm

当我运行命令
top-b-n1 | grep Cpu
时,它总是返回相同的值:

Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
但是当我删除
-n1
部分时,结果以

Cpu(s): 0.3%us, 0.3%sy, 0.0%ni, 99.4%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st 
随后的值就不同了


有什么原因吗?如何获取不同的值?

检查您的
主页:

   The top command calculates Cpu(s) by looking at  the  change  in
   CPU  time  values between samples. When you first run it, it has
   no previous sample to compare to, so these  initial  values  are
   the percentages since boot. It means you need at least two loops
   or you have to ignore summary output from the first loop.   This
   is  problem  for  example  for  batch  mode. There is a possible
   workaround if you define the CPULOOP=1 environment variable. The
   top  command  will  be  run  one  extra hidden loop for CPU data
   before standard output.

您定义选项
-n max
将允许top执行
max
迭代。如果您给出了
1
,则只能从1次迭代中获得数据。当然,还要得到单行数据

我不知道你会如何使用输出
top-b-nx | grep Cpu
可以根据
top
的版本提供不同的输出,例如,在我的Archlinux上,
top-b-n1 | grep Cpu
的输出为:

kent$ (master|…) top -b -n 1 |grep Cpu
%Cpu0  :  12.7/4.1    17[|||||||||||                                                    ]
%Cpu1  :  69.9/17.3   87[|||||||||||||||||||||||||||||||||||||||||||||||||||||||        ]
%Cpu2  :  69.0/19.2   88[|||||||||||||||||||||||||||||||||||||||||||||||||||||||        ]
%Cpu3  :  68.1/18.0   86[||||||||||||||||||||||||||||||||||||||||||||||||||||||         ]

您是否查找了
-n1
选项的功能?如果你这样做了,答案将是显而易见的。但是这个信息在bugs部分提到,而不是在primary部分。谢谢。