![Solaris에서 수학 연산을 수행할 때 awk가 중단됩니다.](https://linux55.com/image/8441/Solaris%EC%97%90%EC%84%9C%20%EC%88%98%ED%95%99%20%EC%97%B0%EC%82%B0%EC%9D%84%20%EC%88%98%ED%96%89%ED%95%A0%20%EB%95%8C%20awk%EA%B0%80%20%EC%A4%91%EB%8B%A8%EB%90%A9%EB%8B%88%EB%8B%A4..png)
저는 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
Solaris에서 awk 명령이 중단되는 이유는 무엇입니까? 무엇을 다르게 해야 합니까?
이것은 스크립트의 큰 보기입니다. 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}')