如何从命令行获取 Linux 中的 CPU/内核数?

问题描述:

我有这个脚本,但我不知道如何获取打印输出中的最后一个元素:

I have this script, but I do not know how to get the last element in the printout:

cat /proc/cpuinfo | awk '/^processor/{print $3}'

最后一个元素应该是 CPU 的数量,减 1.

The last element should be the number of CPUs, minus 1.

grep -c ^processor /proc/cpuinfo

将计算以processor"开头的行数;在 /proc/cpuinfo

will count the number of lines starting with "processor" in /proc/cpuinfo

对于具有超线程的系统,您可以使用

For systems with hyper-threading, you can use

grep ^cpu\scores /proc/cpuinfo | uniq |  awk '{print $4}'

哪个应该返回(例如)8(而上面的命令将返回16)

which should return (for example) 8 (whereas the command above would return 16)