help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mac bash script help


From: Eric Peterson
Subject: mac bash script help
Date: Wed, 9 Sep 2020 18:01:41 -0700

Greetings all, 

  I was using lscpu on a linux system and thought what about my mac.  So I put 
this together using sysctl and system_profiler to get similar output that lscpu 
has.

  Take a look and see if did things in a "proper" manner or at least in a nice 
style. :). I guess this could be expanded to take into account other non-linux 
systems that don't have lscpu command.

  I don't have any fellow mac bash scripters around me anymore, so I thought 
I'd ask the greater world for their thoughts.

  I've gone to the trouble of keeping bash updated.

edp$ echo ${BASH_VERSION}
5.0.17(2)-release


  Bonus question:  Does any know what the CPU Family/Model values relate to?  
What does CPU Family = 6 mean?  Is there a lookup table somewhere with all 
possible values?


Thanks

Eric




#! /usr/bin/env bash
# ========== ========== ========== ========== ========== ========== ========== 
==========
# Emulate linux 'lscpu'.  Doesn't have all the descriptions/values of lscpu.  
Not even
# sure if all of these are valid, i.e. min/max CPU MHz values don't change.
#
# Note: See sample output at bottom of this file after the exit.
#
# Note: Use of piping through xargs does string trimming of any extra spaces 
before
#       and after the value.  Some of these returned values will have 
leading/trailing spaces.
#
# TODO: add error trap, in case something goes wrong?
# TODO: colorize text?
# TODO: add command line arguments?
#         --version to show script version
#         --verbose to show all values, show only some of these for normal use
#         --help for some useful man documentation
#
# Log:
# 2020-09-09 Eric D Peterson       Initial Version
# ========== ========== ========== ========== ========== ========== ========== 
==========

# ---------- ---------- ----------
# only need access to these bin directories.
# this also will bypass any defined aliases in .bashrc i.e. alias grep='grep 
-n'.
# ---------- ---------- ----------
export PATH='/usr/sbin:/usr/bin'

typeset -i EX_OK=0;       readonly EX_OK
typeset -i EX_ERROR=1;    readonly EX_ERROR
typeset VERSION='1.2';    readonly VERSION

# ---------- ---------- ----------
# only run on Mac! Not really needed, but check anyway.
# ---------- ---------- ----------
if [[ "$( uname -s )" != 'Darwin' ]]
then

  echo 'ERROR: This script is only for Mac systems' >&2
  exit "${EX_ERROR}"

fi

# ---------- ---------- ----------
printf "%-22s: %s\n" 'Architecture' "$( uname -m )"

# ---------- ---------- ----------
# Not sure why I look this up.  Mac is always Little Endian (I think).
# ---------- ---------- ----------
v="$( sysctl -n hw.byteorder | xargs )"
if [[ "${v}" = '1234' ]]
then

  value='Little Endian'

else

  value='Big Endian'

fi
printf "%-22s: %s\n" 'Byte Order' "${value}"

# ---------- ---------- ----------
printf "%-22s: %s\n" 'Number of Threads' "$( sysctl -n hw.logicalcpu )"

# ---------- ---------- ----------
printf "%-22s: %s\n" 'Number of Processors' "$( system_profiler 
SPHardwareDataType | grep 'Number of Processors: ' | cut -f2 -d ':' )"

# ---------- ---------- ----------
printf "%-22s: %s\n" 'Number of Cores' "$( system_profiler SPHardwareDataType | 
grep 'Total Number of Cores: ' | cut -f2 -d ':' )"

# ---------- ---------- ----------
printf "%-22s: %s\n" 'Vendor' "$( sysctl -n machdep.cpu.vendor )"

# ---------- ---------- ----------
value="$( sysctl -n machdep.cpu.family | xargs )"
printf "%-22s: %s\n" 'CPU Family' "${value}"

# ---------- ---------- ----------
value="$( sysctl -n machdep.cpu.model | xargs )"
printf "%-22s: %s\n" 'CPU Model' "${value}"

# ---------- ---------- ----------
value="$( sysctl -n machdep.cpu.brand_string | xargs )"
printf "%-22s: %s\n" 'Model Name' "${value}"

# ---------- ---------- ----------
value="$( sysctl -n machdep.cpu.stepping | xargs )"
printf "%-22s: %s\n" 'Stepping' "${value}"

# ---------- ---------- ----------
value="$(( $( sysctl -n -n hw.cpufrequency ) / 1000000 ))"
printf "%-22s: %s\n" 'CPU MHz' "${value}"

  # ---------- ---------- ----------
  # EDP: Not showing these following values from sysctl as they never change.
  #      Noted on some blogs that this is the system's predefined
  #      values, not the actual min/max values the system has used over time.
  # ---------- ---------- ----------

  # ---------- ---------- ----------
  #value="$(( $( sysctl -n -n hw.cpufrequency_mavalue ) / 1000000 ))"
  #printf "%-22s: %s\n" 'CPU max MHz' "${value}"

  # ---------- ---------- ----------
  #value="$(( $( sysctl -n -n hw.cpufrequency_min ) / 1000000 ))"
  #printf "%-22s: %s\n" 'CPU min MHz' "${value}"

# ---------- ---------- ----------
value="$( system_profiler SPHardwareDataType | grep 'L2 Cache (per Core): ' | 
cut -f2 -d ':' | xargs )"
printf "%-22s: %s\n" 'L2 Cache (per Core)' "${value}"

# ---------- ---------- ----------
value="$( system_profiler SPHardwareDataType | grep 'L3 Cache: ' | cut -f2 -d 
':' | xargs )"
printf "%-22s: %s\n" 'L3 Cache' "${value}"

# ---------- ---------- ----------
# Add spacing for subsequent lines so it all lines up on the right side with 
the other values.
# See sample below.
# ---------- ---------- ----------
s='                        '
value="$( sysctl -n machdep.cpu.features | xargs | fold -s -w 60 | sed -e 
"2,\$s/^/${s}/" )"
printf "%-22s: %s\n" 'Flags' "${value}"

exit "${EX_OK}"

# ========== ========== ========== ========== ========== ========== ========== 
==========


# ---------- ---------- ----------
# sample output
# ---------- ---------- ----------
: <<'EOT'
edp$ lscpu
Architecture          : x86_64
Byte Order            : Little Endian
Number of Threads     : 12
Number of Processors  : 1
Number of Cores       : 6
Vendor                : GenuineIntel
CPU Family            : 6
CPU Model             : 158
Model Name            : Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Stepping              : 10
CPU MHz               : 2600
L2 Cache (per Core)   : 256 KB
L3 Cache              : 9 MB
Flags                 : FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE 
MCA 
                        CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT 
TM 
                        PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX SMX EST TM2 
SSSE3 
                        FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES 
                        PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
EOT


# ========== ========== ========== ========== ========== ========== ========== 
==========




reply via email to

[Prev in Thread] Current Thread [Next in Thread]