我正在 SunOS 5.11/Solaris 11.3 计算机上工作。我有一个计算和导出 CPU 频率的 bash 脚本,因为我在一些测试脚本中经常使用它。
这是感兴趣的两条线:
solaris:~$ CPU_FREQ=$(psrinfo -v 2>/dev/null | grep 'MHz' | head -1 | awk '{print $6}')
solaris:~$ echo $CPU_FREQ
3000
solaris:~$ CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024/1024}")
^C
为什么 awk 命令在 Solaris 下挂起?我应该做些什么不同的事情?
这是脚本的大视图。它在 Linux、OS X 和 BSD 下运行良好。
IS_LINUX=$(uname -s | grep -i -c linux)
IS_DARWIN=$(uname -s | grep -i -c darwin)
IS_SOLARIS=$(uname -s | grep -i -c sunos)
# 2.0 GHz or 2.0/1024/1024/1024
CPU_FREQ=1.8189894
if [ "$IS_LINUX" -ne "0" ] && [ -e "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq" ]; then
CPU_FREQ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq)
CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024/1024}")
elif [ "$IS_DARWIN" -ne "0" ]; then
CPU_FREQ=$(sysctl -a 2>/dev/null | grep 'hw.cpufrequency' | head -1 | awk '{print $3}')
CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024/1024/1024}")
elif [ "$IS_SOLARIS" -ne "0" ]; then
CPU_FREQ=$(psrinfo -v 2>/dev/null | grep 'MHz' | head -1 | awk '{print $6}')
CPU_FREQ=$(awk "BEGIN {print $CPU_FREQ/1024}")
fi
# Used by Crypto++ benchmarks
export CPU_SPEED=$CPU_FREQ
这是输出psrinfo
:
$ psrinfo -v
Status of virtual processor 0 as of: 06/07/2016 18:23:29
on-line since 06/07/2016 14:28:28.
The i386 processor operates at 3000 MHz,
and has an i387 compatible floating point processor.
Status of virtual processor 1 as of: 06/07/2016 18:23:29
on-line since 06/07/2016 14:28:34.
The i386 processor operates at 3000 MHz,
and has an i387 compatible floating point processor.
Status of virtual processor 2 as of: 06/07/2016 18:23:29
on-line since 06/07/2016 14:28:34.
The i386 processor operates at 3000 MHz,
and has an i387 compatible floating point processor.
Status of virtual processor 3 as of: 06/07/2016 18:23:29
on-line since 06/07/2016 14:28:34.
The i386 processor operates at 3000 MHz,
and has an i387 compatible floating point processor.
答案1
nawk
在 Solaris 下使用。
/usr/bin/awk
是遗留的、非 POSIX 的awk
,仅包含 BEGIN 操作的脚本不会跳过其stdin
.
以下声明出现在nawk
和/usr/xpg4/bin/awk
手册中,但在旧手册中没有awk
:
If an nawk program consists of only actions with the pattern BEGIN, and
the BEGIN action contains no getline function, nawk exits without read-
ing its input when the last statement in the last BEGIN action is exe-
cuted.
顺便说一句,不需要运行head
两个grep, and
脚本awk
。一个awk
脚本可以单独完成所有这些工作:
CPU_FREQ=$(psrinfo -v 2>/dev/null | nawk '/MHz/ {print $6/1024;exit}')