前端之家收集整理的这篇文章主要介绍了
获取CPU信息的shell脚本,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#!/usr/bin/bash #filename:
cpu-info.sh #this sctrpy only works in a Linux system which has one or more identical physical
cpu(s). #逻辑
cpu个数 echo -n "logical
cpu number in total: " cat /proc/
cpuinfo | grep "processor" | wc -l #有些系统没有多核也没有打开多线程, 直接
退出 cat /proc/
cpuinfo | grep -qi "core id" if [ $? -ne 0 ]; then echo "Warning: No multi-core or hyper-threading is enabled." ecit 0; fi #物理
cpu个数 echo -n "physical
cpu number in total: " cat /proc/
cpuinfo | grep "physical id" | sort | uniq | wc -l #每个物理
cpu上Core的个数(未计入超线程) echo -n "core number in a physical
cpu: " core_per_phy_
cpu=$(cat /proc/
cpuinfo | grep "core id" | sort | uniq | wc -l) echo $core_per_phy_
cpu #每个物理
cpu中逻辑
cpu(可能是core, threads 或both)的个数 echo -n "logical
cpu number in a physical
cpu: " logical_
cpu_per_phy_
cpu=$(cat /proc/
cpuinfo | grep "siblings" | sort | uniq | awk -F: '{print $2}') echo $logical_
cpu_per_phy_
cpu #是否打开有超线程 #如果在同一个物理
cpu上有两个逻辑
cpu具有相同的“core id”, 那么超线程是打开的 #此处根据前面计算的logical_
cpu_per_phy_
cpu和core_per_phy_
cpu的比较来查看超线程 if [ $logical_
cpu_per_phy_
cpu -gt $core_per_phy_
cpu ]; then echo "Hyper threading is enabled." elif [ $logical_
cpu_per_phy_
cpu -eq $core_per_phy_
cpu ]; then echo "Hyper threading is NOT enabled." else echo "Error. There's something wrong." fi