-
-
Save felipepodesta/4af1bdfceb951c3aa1e6211e0fbec709 to your computer and use it in GitHub Desktop.
A script to print system information.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Created by Dylan Araps, modified by Roger Fu | |
# Copyright (c) 2016-2017 Dylan Araps | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the 'Software'), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
bash_version="${BASH_VERSION/.*}" | |
sys_locale="${LANG:-C}" | |
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-${HOME}/.config}" | |
old_ifs="$IFS" | |
# Speed up script by not using unicode. | |
export LC_ALL=C | |
export LANG=C | |
# Add more paths to $PATH. | |
export PATH="/usr/xpg4/bin:/usr/sbin:/sbin:/usr/etc:/usr/libexec:${PATH}" | |
# Set no case match. | |
# shopt -s nocasematch | |
# Reset colors and bold. | |
reset="\\e[0m" | |
# DETECT INFORMATION | |
get_os() { | |
# $kernel_name is set in a function called cache_uname and is | |
# just the output of "uname -s". | |
case "$kernel_name" in | |
"Linux" | "GNU"*) os="Linux" ;; | |
"Darwin") os="$(sw_vers -productName)" ;; | |
*"BSD" | "DragonFly" | "Bitrig") os="BSD" ;; | |
"CYGWIN"* | "MSYS"* | "MINGW"*) os="Windows" ;; | |
"SunOS") os="Solaris" ;; | |
"Haiku") os="Haiku" ;; | |
"MINIX") os="MINIX" ;; | |
"AIX") os="AIX" ;; | |
"IRIX"*) os="IRIX" ;; | |
"FreeMiNT") os="FreeMiNT" ;; | |
*) | |
printf "%s\n" "Unknown OS detected: '$kernel_name', aborting..." >&2 | |
printf "%s\n" "Open an issue on GitHub to add support for your OS." >&2 | |
exit 1 | |
;; | |
esac | |
} | |
get_distro() { | |
[[ "$distro" ]] && return | |
case "$os" in | |
"Linux" | "BSD" | "MINIX") | |
if [[ "$(< /proc/version)" == *"Microsoft"* || | |
"$kernel_version" == *"Microsoft"* ]]; then | |
case "$distro_shorthand" in | |
"on") distro="$(lsb_release -sir) [Windows 10]" ;; | |
"tiny") distro="Windows 10" ;; | |
*) distro="$(lsb_release -sd) on Windows 10" ;; | |
esac | |
elif [[ "$(< /proc/version)" == *"chrome-bot"* || -f "/dev/cros_ec" ]]; then | |
case "$distro_shorthand" in | |
"on") distro="$(lsb_release -sir) [Chrome OS]" ;; | |
"tiny") distro="Chrome OS" ;; | |
*) distro="$(lsb_release -sd) on Chrome OS" ;; | |
esac | |
elif [[ -f "/etc/redstar-release" ]]; then | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="Red Star OS" ;; | |
*) distro="Red Star OS $(awk -F'[^0-9*]' '$0=$2' /etc/redstar-release)" | |
esac | |
elif [[ -f "/etc/siduction-version" ]]; then | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="Siduction" ;; | |
*) distro="Siduction ($(lsb_release -sic))" | |
esac | |
elif type -p lsb_release >/dev/null; then | |
case "$distro_shorthand" in | |
"on") lsb_flags="-sir" ;; | |
"tiny") lsb_flags="-si" ;; | |
*) lsb_flags="-sd" ;; | |
esac | |
distro="$(lsb_release $lsb_flags)" | |
elif [[ -f "/etc/GoboLinuxVersion" ]]; then | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="GoboLinux" ;; | |
*) distro="GoboLinux $(< /etc/GoboLinuxVersion)" | |
esac | |
elif type -p guix >/dev/null; then | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="GuixSD" ;; | |
*) distro="GuixSD $(guix system -V | awk 'NR==1{printf $5}')" | |
esac | |
elif type -p crux >/dev/null; then | |
distro="$(crux)" | |
case "$distro_shorthand" in | |
"on") distro="${distro//version}" ;; | |
"tiny") distro="${distro//version*}" ;; | |
esac | |
elif type -p tazpkg >/dev/null; then | |
distro="SliTaz $(< /etc/slitaz-release)" | |
elif type -p kpt >/dev/null && \ | |
type -p kpm >/dev/null; then | |
distro="KSLinux" | |
elif [[ -d "/system/app/" && -d "/system/priv-app" ]]; then | |
distro="Android $(getprop ro.build.version.release)" | |
elif [[ -f "/etc/os-release" || \ | |
-f "/usr/lib/os-release" || \ | |
-f "/etc/openwrt_release" ]]; then | |
files=("/etc/os-release" "/usr/lib/os-release" "/etc/openwrt_release") | |
# Source the os-release file | |
for file in "${files[@]}"; do | |
source "$file" && break | |
done | |
# Format the distro name. | |
case "$distro_shorthand" in | |
"on") distro="${NAME:-${DISTRIB_ID}} ${VERSION_ID:-${DISTRIB_RELEASE}}" ;; | |
"tiny") distro="${NAME:-${DISTRIB_ID:-${TAILS_PRODUCT_NAME}}}" ;; | |
"off") distro="${PRETTY_NAME:-${DISTRIB_DESCRIPTION}} ${UBUNTU_CODENAME}" ;; | |
esac | |
# Workarounds for distros that go against the os-release standard. | |
[[ -z "${distro// }" ]] && distro="$(awk '/BLAG/ {print $1; exit}')" "${files[@]}" | |
[[ -z "${distro// }" ]] && distro="$(awk -F'=' '{print $2; exit}')" "${files[@]}" | |
else | |
for release_file in /etc/*-release; do | |
distro+="$(< "$release_file")" | |
done | |
if [[ -z "$distro" ]]; then | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="$kernel_name" ;; | |
*) distro="$kernel_name $kernel_version" ;; | |
esac | |
distro="${distro/DragonFly/DragonFlyBSD}" | |
# Workarounds for FreeBSD based distros. | |
[[ -f "/etc/pcbsd-lang" ]] && distro="PCBSD" | |
[[ -f "/etc/trueos-lang" ]] && distro="TrueOS" | |
# /etc/pacbsd-release is an empty file | |
[[ -f "/etc/pacbsd-release" ]] && distro="PacBSD" | |
fi | |
fi | |
distro="$(trim_quotes "$distro")" | |
distro="${distro/'NAME='}" | |
;; | |
"Mac OS X") | |
osx_version="$(sw_vers -productVersion)" | |
osx_build="$(sw_vers -buildVersion)" | |
case "$osx_version" in | |
"10.4"*) codename="Mac OS X Tiger" ;; | |
"10.5"*) codename="Mac OS X Leopard" ;; | |
"10.6"*) codename="Mac OS X Snow Leopard" ;; | |
"10.7"*) codename="Mac OS X Lion" ;; | |
"10.8"*) codename="OS X Mountain Lion" ;; | |
"10.9"*) codename="OS X Mavericks" ;; | |
"10.10"*) codename="OS X Yosemite" ;; | |
"10.11"*) codename="OS X El Capitan" ;; | |
"10.12"*) codename="macOS Sierra" ;; | |
"10.13"*) codename="macOS High Sierra" ;; | |
*) codename="macOS" ;; | |
esac | |
distro="$codename $osx_version $osx_build" | |
case "$distro_shorthand" in | |
"on") distro="${distro/ ${osx_build}}" ;; | |
"tiny") | |
case "$osx_version" in | |
"10."[4-7]*) distro="${distro/${codename}/Mac OS X}" ;; | |
"10."[8-9]* | "10.1"[0-1]*) distro="${distro/${codename}/OS X}" ;; | |
"10.1"[2-3]*) distro="${distro/${codename}/macOS}" ;; | |
esac | |
distro="${distro/ ${osx_build}}" | |
;; | |
esac | |
;; | |
"iPhone OS") | |
distro="iOS $(sw_vers -productVersion)" | |
# "uname -m" doesn't print architecture on iOS so we force it off. | |
os_arch="off" | |
;; | |
"Windows") | |
distro="$(wmic os get Caption)" | |
# Strip crap from the output of wmic. | |
distro="${distro/Caption}" | |
distro="${distro/Microsoft }" | |
;; | |
"Solaris") | |
case "$distro_shorthand" in | |
"on" | "tiny") distro="$(awk 'NR==1{print $1 " " $3;}' /etc/release)" ;; | |
*) distro="$(awk 'NR==1{print $1 " " $2 " " $3;}' /etc/release)" ;; | |
esac | |
distro="${distro/\(*}" | |
;; | |
"Haiku") | |
distro="$(uname -sv | awk '{print $1 " " $2}')" | |
;; | |
"AIX") | |
distro="AIX $(oslevel)" | |
;; | |
"IRIX") | |
distro="IRIX ${kernel_version}" | |
;; | |
"FreeMiNT") | |
distro="FreeMiNT" | |
;; | |
esac | |
distro="${distro//Enterprise Server}" | |
[[ -z "$distro" ]] && distro="$os (Unknown)" | |
# Get OS architecture. | |
case "$os" in | |
"Solaris" | "AIX" | "Haiku" | "IRIX" | "FreeMiNT") machine_arch="$(uname -p)" ;; | |
*) machine_arch="$(uname -m)" ;; | |
esac | |
distro+=" ${machine_arch}" | |
[[ "${ascii_distro:-auto}" == "auto" ]] && \ | |
ascii_distro="$(trim "$distro")" | |
} | |
get_model() { | |
case "$os" in | |
"Linux") | |
if [[ -d "/system/app/" && -d "/system/priv-app" ]]; then | |
model="$(getprop ro.product.brand) $(getprop ro.product.model)" | |
elif [[ -f /sys/devices/virtual/dmi/id/product_name || | |
-f /sys/devices/virtual/dmi/id/product_version ]]; then | |
model="$(< /sys/devices/virtual/dmi/id/product_name)" | |
model+=" $(< /sys/devices/virtual/dmi/id/product_version)" | |
elif [[ -f /sys/firmware/devicetree/base/model ]]; then | |
model="$(< /sys/firmware/devicetree/base/model)" | |
elif [[ -f /tmp/sysinfo/model ]]; then | |
model="$(< /tmp/sysinfo/model)" | |
fi | |
;; | |
"Mac OS X") | |
if [[ "$(kextstat | grep "FakeSMC")" != "" ]]; then | |
model="Hackintosh (SMBIOS: $(sysctl -n hw.model))" | |
else | |
model="$(sysctl -n hw.model)" | |
fi | |
;; | |
"iPhone OS") | |
case "$machine_arch" in | |
"iPad1,1") model="iPad" ;; | |
"iPad2,"[1-4]) model="iPad 2" ;; | |
"iPad3,"[1-3]) model="iPad 3" ;; | |
"iPad3,"[4-6]) model="iPad 4" ;; | |
"iPad6,11" | "iPad 6,12") model="iPad 5" ;; | |
"iPad4,"[1-3]) model="iPad Air" ;; | |
"iPad5,"[3-4]) model="iPad Air 2" ;; | |
"iPad6,"[7-8]) model="iPad Pro (12.9 Inch)" ;; | |
"iPad6,"[3-4]) model="iPad Pro (9.7 Inch)" ;; | |
"iPad7,"[1-2]) model="iPad Pro 2 (12.9 Inch)" ;; | |
"iPad7,"[3-4]) model="iPad Pro (10.5 Inch)" ;; | |
"iPad2,"[5-7]) model="iPad mini" ;; | |
"iPad4,"[4-6]) model="iPad mini 2" ;; | |
"iPad4,"[7-9]) model="iPad mini 3" ;; | |
"iPad5,"[1-2]) model="iPad mini 4" ;; | |
"iPhone1,1") model="iPhone" ;; | |
"iPhone1,2") model="iPhone 3G" ;; | |
"iPhone2,1") model="iPhone 3GS" ;; | |
"iPhone3,"[1-3]) model="iPhone 4" ;; | |
"iPhone4,1") model="iPhone 4S" ;; | |
"iPhone5,"[1-2]) model="iPhone 5" ;; | |
"iPhone5,"[3-4]) model="iPhone 5c" ;; | |
"iPhone6,"[1-2]) model="iPhone 5s" ;; | |
"iPhone7,2") model="iPhone 6" ;; | |
"iPhone7,1") model="iPhone 6 Plus" ;; | |
"iPhone8,1") model="iPhone 6s" ;; | |
"iPhone8,2") model="iPhone 6s Plus" ;; | |
"iPhone8,4") model="iPhone SE" ;; | |
"iPhone9,1" | "iPhone9,3") model="iPhone 7" ;; | |
"iPhone9,2" | "iPhone9,4") model="iPhone 7 Plus" ;; | |
"iPhone10,1" | "iPhone10,4") model="iPhone 8" ;; | |
"iPhone10,2" | "iPhone10,5") model="iPhone 8 Plus" ;; | |
"iPhone10,3" | "iPhone10,6") model="iPhone X" ;; | |
"iPod1,1") model="iPod touch" ;; | |
"ipod2,1") model="iPod touch 2G" ;; | |
"ipod3,1") model="iPod touch 3G" ;; | |
"ipod4,1") model="iPod touch 4G" ;; | |
"ipod5,1") model="iPod touch 5G" ;; | |
"ipod7,1") model="iPod touch 6G" ;; | |
esac | |
;; | |
"BSD" | "MINIX") | |
model="$(sysctl -n hw.vendor hw.product)" | |
;; | |
"Windows") | |
model="$(wmic computersystem get manufacturer,model)" | |
model="${model/Manufacturer}" | |
model="${model/Model}" | |
;; | |
"Solaris") | |
model="$(prtconf -b | awk -F':' '/banner-name/ {printf $2}')" | |
;; | |
"AIX") | |
model="$(/usr/bin/uname -M)" | |
;; | |
"FreeMiNT") | |
model="$(sysctl -n hw.model)" | |
;; | |
esac | |
# Remove dummy OEM info. | |
model="${model//To be filled by O.E.M.}" | |
model="${model//To Be Filled*}" | |
model="${model//OEM*}" | |
model="${model//Not Applicable}" | |
model="${model//System Product Name}" | |
model="${model//System Version}" | |
model="${model//Undefined}" | |
model="${model//Default string}" | |
model="${model//Not Specified}" | |
model="${model//Type1ProductConfigId}" | |
model="${model//INVALID}" | |
model="${model//�}" | |
case "$model" in | |
"Standard PC"*) model="KVM/QEMU (${model})" ;; | |
esac | |
} | |
get_title() { | |
title="SYSTEM INFORMATION\n------------------" | |
} | |
get_kernel() { | |
# Since these OS are integrated systems, it's better to skip this function altogether | |
[[ "$os" =~ (AIX|IRIX) ]] && return | |
kernel="$kernel_name $kernel_version" | |
} | |
get_cpu() { | |
# NetBSD emulates the Linux /proc filesystem instead of using sysctl for hw | |
# information so we have to use this block below which temporarily sets the | |
# OS to "Linux" for the duration of this function. | |
[[ "$distro" == "NetBSD"* ]] && local os="Linux" | |
case "$os" in | |
"Linux" | "MINIX" | "Windows") | |
# Get CPU name. | |
cpu_file="/proc/cpuinfo" | |
case "$machine_arch" in | |
"frv" | "hppa" | "m68k" | "openrisc" | "or"* | "powerpc" | "ppc"* | "sparc"*) | |
cpu="$(awk -F':' '/^cpu\t|^CPU/ {printf $2; exit}' "$cpu_file")" | |
;; | |
"s390"*) | |
cpu="$(awk -F'=' '/machine/ {print $4; exit}' "$cpu_file")" | |
;; | |
"ia64" | "m32r") | |
cpu="$(awk -F':' '/model/ {print $2; exit}' "$cpu_file")" | |
[[ -z "$cpu" ]] && cpu="$(awk -F':' '/family/ {printf $2; exit}' "$cpu_file")" | |
;; | |
*) | |
cpu="$(awk -F ': | @' '/model name|Processor|^cpu model|chip type|^cpu type/\ | |
{printf $2; exit}' "$cpu_file")" | |
[[ "$cpu" == *"processor rev"* ]] && \ | |
cpu="$(awk -F':' '/Hardware/ {print $2; exit}' "$cpu_file")" | |
;; | |
esac | |
speed_dir="/sys/devices/system/cpu/cpu0/cpufreq" | |
# Get CPU speed. | |
if [[ -d "$speed_dir" ]]; then | |
# Fallback to bios_limit if $speed_type fails. | |
speed="$(< "${speed_dir}/${speed_type}")" ||\ | |
speed="$(< "${speed_dir}/bios_limit")" ||\ | |
speed="$(< "${speed_dir}/scaling_max_freq")" ||\ | |
speed="$(< "${speed_dir}/cpuinfo_max_freq")" | |
speed="$((speed / 1000))" | |
else | |
speed="$(awk -F ': |\\.' '/cpu MHz|^clock/ {printf $2; exit}' "$cpu_file")" | |
speed="${speed/MHz}" | |
fi | |
# Get CPU cores. | |
case "$cpu_cores" in | |
"logical" | "on") cores="$(grep -c "^processor" "$cpu_file")" ;; | |
"physical") cores="$(grep "^core id" "$cpu_file" | sort -u | wc -l)" ;; | |
esac | |
;; | |
"Mac OS X") | |
cpu="$(sysctl -n machdep.cpu.brand_string)" | |
# Get CPU cores. | |
case "$cpu_cores" in | |
"logical" | "on") cores="$(sysctl -n hw.logicalcpu_max)" ;; | |
"physical") cores="$(sysctl -n hw.physicalcpu_max)" ;; | |
esac | |
;; | |
"iPhone OS") | |
case "$machine_arch" in | |
"iPhone1,"[1-2] | "iPod1,1") cpu="Samsung S5L8900 (1) @ 412MHz" ;; | |
"iPhone2,1") cpu="Samsung S5PC100 (1) @ 600MHz" ;; | |
"iPhone3,"[1-3] | "iPod4,1") cpu="Apple A4 (1) @ 800MHz" ;; | |
"iPhone4,1" | "iPod5,1") cpu="Apple A5 (2) @ 800MHz" ;; | |
"iPhone5,"[1-4]) cpu="Apple A6 (2) @ 1.3GHz" ;; | |
"iPhone6,"[1-2]) cpu="Apple A7 (2) @ 1.3GHz" ;; | |
"iPhone7,"[1-2]) cpu="Apple A8 (2) @ 1.4GHz" ;; | |
"iPhone8,"[1-4]) cpu="Apple A9 (2) @ 1.85GHz" ;; | |
"iPhone9,"[1-4]) cpu="Apple A10 Fusion (4) @ 2.34GHz" ;; | |
"iPod2,1") cpu="Samsung S5L8720 (1) @ 533MHz" ;; | |
"iPod3,1") cpu="Samsung S5L8922 (1) @ 600MHz" ;; | |
"iPod7,1") cpu="Apple A8 (2) @ 1.1GHz" ;; | |
"iPad1,1") cpu="Apple A4 (1) @ 1GHz" ;; | |
"iPad2,"[1-7]) cpu="Apple A5 (2) @ 1GHz" ;; | |
"iPad3,"[1-3]) cpu="Apple A5X (2) @ 1GHz" ;; | |
"iPad3,"[4-6]) cpu="Apple A6X (2) @ 1.4GHz" ;; | |
"iPad4,"[1-3]) cpu="Apple A7 (2) @ 1.4GHz" ;; | |
"iPad4,"[4-9]) cpu="Apple A7 (2) @ 1.4GHz" ;; | |
"iPad5,"[1-2]) cpu="Apple A8 (2) @ 1.5GHz" ;; | |
"iPad5,"[3-4]) cpu="Apple A8X (3) @ 1.5GHz" ;; | |
"iPad6,"[3-4]) cpu="Apple A9X (2) @ 2.16GHz" ;; | |
"iPad6,"[7-8]) cpu="Apple A9X (2) @ 2.26GHz" ;; | |
esac | |
;; | |
"BSD") | |
# Get CPU name. | |
cpu="$(sysctl -n hw.model)" | |
cpu="${cpu/[0-9]\.*}" | |
cpu="${cpu/ @*}" | |
# Get CPU speed. | |
speed="$(sysctl -n hw.cpuspeed)" | |
[[ -z "$speed" ]] && speed="$(sysctl -n hw.clockrate)" | |
# Get CPU cores. | |
cores="$(sysctl -n hw.ncpu)" | |
;; | |
"Solaris") | |
# Get CPU name. | |
cpu="$(psrinfo -pv)" | |
cpu="${cpu//*$'\n'}" | |
cpu="${cpu/[0-9]\.*}" | |
cpu="${cpu/ @*}" | |
cpu="${cpu/\(portid*}" | |
# Get CPU speed. | |
speed="$(psrinfo -v | awk '/operates at/ {print $6; exit}')" | |
# Get CPU cores. | |
case "$cpu_cores" in | |
"logical" | "on") cores="$(kstat -m cpu_info | grep -c -F "chip_id")" ;; | |
"physical") cores="$(psrinfo -p)" ;; | |
esac | |
;; | |
"Haiku") | |
# Get CPU name. | |
cpu="$(sysinfo -cpu | awk -F '\\"' '/CPU #0/ {print $2}')" | |
cpu="${cpu/@*}" | |
# Get CPU speed. | |
speed="$(sysinfo -cpu | awk '/running at/ {print $NF; exit}')" | |
speed="${speed/MHz}" | |
# Get CPU cores. | |
cores="$(sysinfo -cpu | grep -c -F 'CPU #')" | |
;; | |
"AIX") | |
# Get CPU name. | |
cpu="$(lsattr -El proc0 -a type | awk '{printf $2}')" | |
# Get CPU speed. | |
speed="$(prtconf -s | awk -F':' '{printf $2}')" | |
speed="${speed/MHz}" | |
# Get CPU cores. | |
case "$cpu_cores" in | |
"logical" | "on") | |
cores="$(lparstat -i | awk -F':' '/Online Virtual CPUs/ {printf $2}')" | |
;; | |
"physical") cores="$(lparstat -i | awk -F':' '/Active Physical CPUs/ {printf $2}')" | |
esac | |
;; | |
"IRIX") | |
# Get CPU name. | |
cpu="$(hinv -c processor | awk -F':' '/CPU:/ {printf $2}')" | |
# Get CPU speed. | |
speed="$(hinv -c processor | awk '/MHZ/ {printf $2}')" | |
# Get CPU cores. | |
cores="$(sysconf NPROC_ONLN)" | |
;; | |
"FreeMiNT") | |
cpu="$(awk -F':' '/CPU:/ {printf $2}' /kern/cpuinfo)" | |
speed="$(awk -F '[:.M]' '/Clocking:/ {printf $2}' /kern/cpuinfo)" | |
;; | |
esac | |
# Remove un-needed patterns from cpu output. | |
cpu="${cpu//(TM)}" | |
cpu="${cpu//(tm)}" | |
cpu="${cpu//(R)}" | |
cpu="${cpu//(r)}" | |
cpu="${cpu//CPU}" | |
cpu="${cpu//Processor}" | |
cpu="${cpu//Dual-Core}" | |
cpu="${cpu//Quad-Core}" | |
cpu="${cpu//Six-Core}" | |
cpu="${cpu//Eight-Core}" | |
cpu="${cpu//, * Compute Cores}" | |
cpu="${cpu//Core}" | |
cpu="${cpu//(\"AuthenticAMD\"*)}" | |
cpu="${cpu//with Radeon * Graphics}" | |
cpu="${cpu//, altivec supported}" | |
cpu="${cpu//FPU*}" | |
cpu="${cpu//Chip Revision*}" | |
cpu="${cpu//Technologies, Inc}" | |
# Trim spaces from core and speed output | |
cores="${cores//[[:space:]]}" | |
speed="${speed//[[:space:]]}" | |
# Add CPU cores to the output. | |
[[ "$cpu_cores" != "off" && "$cores" ]] && \ | |
case "$os" in | |
"Mac OS X") cpu="${cpu/@/(${cores}) @}" ;; | |
*) cpu="$cpu ($cores)" ;; | |
esac | |
# Add CPU speed to the output. | |
if (( speed < 1000 )); then | |
cpu="$cpu @ ${speed}MHz" | |
else | |
[[ "$speed_shorthand" == "on" ]] && speed="$((speed / 100))" | |
speed="${speed:0:1}.${speed:1}" | |
cpu="$cpu @ ${speed}GHz" | |
fi | |
} | |
get_locale() { | |
locale="$sys_locale" | |
} | |
info() { | |
# Save subtitle value. | |
[[ "$2" ]] && subtitle="$1" | |
# Make sure that $prin is unset. | |
unset -v prin | |
# Call the function. | |
"get_${2:-$1}" | |
# If the get_func function called 'prin' directly, stop here. | |
[[ "$prin" ]] && return | |
# Update the variable. | |
output="$(trim "${!2:-${!1}}")" | |
if [[ "$2" && "${output// }" ]]; then | |
prin "$1" "$output" | |
elif [[ "${output// }" ]]; then | |
prin "$output" | |
else | |
err "Info: Couldn't detect ${1}." | |
fi | |
unset -v subtitle | |
} | |
prin() { | |
# If $2 doesn't exist we format $1 as info. | |
if [[ "$(trim "$1")" && "$2" ]]; then | |
string="${1}${2:+: $2}" | |
else | |
string="${2:-$1}" | |
local subtitle_color="$info_color" | |
fi | |
string="$(trim "${string//$'\\e[0m'}")" | |
# Log length if it doesn't exist. | |
if [[ -z "$length" ]]; then | |
length="$(strip_sequences "$string")" | |
length="${#length}" | |
fi | |
# Format the output. | |
string="${string/:/${reset}${colon_color}:${info_color}}" | |
string="${subtitle_color}${bold}${string}" | |
# Print the info. | |
printf "%b\n" "${text_padding:+\\e[${text_padding}C}${zws}${string}${reset} " | |
# Calculate info height. | |
((++info_height)) | |
# Log that prin was used. | |
prin=1 | |
} | |
get_line_break() { | |
# Print it directly. | |
printf "%b\n" "${zws}" | |
# Calculate info height. | |
((++info_height)) | |
line_breaks+="\n" | |
# Tell info() that we printed manually. | |
prin=1 | |
} | |
trim() { | |
# When a string is passed to "echo" all trailing and leading | |
# whitespace is removed and inside the string multiple spaces are | |
# condensed into single spaces. | |
# | |
# The "set -f/+f" is here so that "echo" doesn't cause any expansion | |
# of special characters. | |
# | |
# The whitespace trim doesn't work with multiline strings so we use | |
# "${1//[[:space:]]/ }" to remove newlines before we trim the whitespace. | |
set -f | |
# shellcheck disable=2086 | |
builtin echo -E ${1//[[:space:]]/ } | |
set +f | |
} | |
trim_quotes() { | |
trim_output="${1//\'}" | |
trim_output="${trim_output//\"}" | |
printf "%s" "$trim_output" | |
} | |
strip_sequences() { | |
strip="${1//$'\\e['3[0-9]m}" | |
strip="${strip//$'\\e['38\;5\;[0-9]m}" | |
strip="${strip//$'\\e['38\;5\;[0-9][0-9]m}" | |
strip="${strip//$'\\e['38\;5\;[0-9][0-9][0-9]m}" | |
printf "%s\n" "$strip" | |
} | |
uppercase() { | |
((bash_version >= 4)) && printf "%s" "${1^}" | |
} | |
# OTHER | |
err() { | |
err+="$(color 1)[!]\\e[0m $1\n" | |
} | |
cache_uname() { | |
# Cache the output of uname so we don't | |
# have to spawn it multiple times. | |
uname=($(uname -sr)) | |
kernel_name="${uname[0]}" | |
kernel_version="${uname[1]}" | |
} | |
get_commit() { | |
if git log &>/dev/null; then | |
commit="$(git log | head -n1)" | |
else | |
commit="This is not the repository you are looking for." | |
fi | |
} | |
print_info() { | |
info title | |
info "OS" distro | |
info "Model" model | |
info "Kernel" kernel | |
info "CPU" cpu | |
info "Latest Commit" commit | |
} | |
main() { | |
distro_shorthand="on" | |
cache_uname | |
get_os | |
exec 2>/dev/null | |
get_distro | |
# [[ "$stdout" == "on" ]] && stdout | |
# Minix doesn't support these sequences. | |
if [[ "$TERM" != "minix" && "$stdout" != "on" ]]; then | |
# If the script exits for any reason, unhide the cursor. | |
trap 'printf "\\e[?25h\\e[?7h"' EXIT | |
# Hide the cursor and disable line wrap. | |
printf "\\e[?25l\\e[?7l" | |
fi | |
print_info | |
return 0 | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment