Solaris에서 수학 연산을 수행할 때 awk가 중단됩니다.

Solaris에서 수학 연산을 수행할 때 awk가 중단됩니다.

저는 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

nawkSolaris에서 사용됩니다.

/usr/bin/awkPOSIX가 아닌 레거시이며 awkBEGIN 작업만 포함하는 스크립트에서는 건너뛰지 않습니다 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}')

관련 정보