Last active
December 1, 2022 00:07
-
-
Save SamantazFox/6866f43774372982cbd1e28533b13bcf to your computer and use it in GitHub Desktop.
A quick set of (almost) one-liner functions to retrieve a Liux/BSD computer's specifications from the shell.
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
# | |
# Distro name | |
# | |
# Ubuntu / Debian | |
if [ -e /etc/lsb-release ]; then | |
. /etc/lsb-release | |
distro="$DISTRIB_ID $DISTRIB_RELEASE $DISTRIB_CODENAME" | |
# RedHat / Fedora / CentOS | |
elif [ -e /etc/redhat-release ]; then distro=$(cat /etc/redhat-release) | |
elif [ -e /etc/system-release ]; then distro=$(cat /etc/system-release) | |
# Arch Linux / Manjaroo | |
elif [ -e /etc/os-release ]; then | |
. /etc/os-release | |
distro="$NAME" | |
# Fallback | |
else | |
distro="Linux generic" | |
fi | |
# | |
# Kernel / Memory / CPU details | |
# | |
kernel="$(uname -m) $(uname -sr)" | |
meminfo=( $(IFS=$'\n' sed -nE ' | |
s/MemTotal:\s+([0-9]+) kB/\1/p | |
s/MemFree:\s+([0-9]+) kB/\1/p | |
s/MemAvailable:\s+([0-9]+) kB/\1/p | |
' /proc/meminfo) ) | |
mem_total_kb="${meminfo[1]}"; mem_total_mb=$(( $mem_total_kb / 1024 )) | |
mem_free_kb="${meminfo[2]}"; mem_free_mb=$(( $mem_free_kb / 1024 )) | |
mem_avail_kb="${meminfo[3]}"; mem_avail_mb=$(( $mem_avail_kb / 1024 )) | |
cpu_name=$(sed -nE '8q; s/model name\s*:\s+([A-Za-z0-9 \-\(\)]+)/\1/p' /proc/cpuinfo) | |
cpu_cores=$(sed -nE '8q; s/cpu cores\s*:\s+([A-Za-z0-9 \-\(\)]+)/\1/p' /proc/cpuinfo) | |
cpu_threads=$(sed -nE '12q; s/siblings\s*:\s+([A-Za-z0-9 \-\(\)]+)/\1/p' /proc/cpuinfo) | |
cpu_freq=$(cat /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq) | |
cpu_freq="${cpu_freq:0:1}.${cpu_freq:1:1}" | |
# | |
# Other | |
# | |
case $SHELL in | |
*zsh) shell=$($SHELL --version | sed -E 's/(\(.*\))//');; | |
*bash) shell=$($SHELL --version | sed -E '1q');; | |
*) shell="Unknown";; | |
esac | |
disks_raw=( $(df -Ph --total --local -x tmpfs -x devtmpfs) ) | |
disks_total=${disks_raw[-5]} | |
disks_used=${disks_raw[-4]} | |
disks_avail=${disks_raw[-3]} | |
if [ -e /bin/glxinfo ] || [ -e /usr/bin/glxinfo ] | |
then | |
IFS=$'\n' glxinfo=( $( glxinfo -B | sed -nE " | |
s|OpenGL vendor string: (.*)|\1|pg | |
s|OpenGL renderer string: ([^/]+).*|\1|pg | |
") ) | |
gpu_vendor="${glxinfo[1]}" | |
gpu_device="${glxinfo[2]}" | |
# Avoid "Intel Open Source Technology Center" | |
case "$gpu_vendor" in | |
Intel*) gpu="Intel $gpu_device";; | |
NVIDIA*) gpu="$gpu_vendor $gpu_device";; | |
esac | |
else | |
# Change IFS to match only newlines in lspci | |
IFS=$'\n' gpu_all=( $( \ | |
lspci -mm | sed -nE '/VGA/{ s/(" "| "|" )/|/g; s/"$//p }' | |
) ) | |
# Internal GPU (iGPU) | |
IFS=$'|' gpu_int=( $(echo ${gpu_all[1]}) ) | |
gpu_int="${gpu_int[3]} $(echo "${gpu_int[4]}" | sed -nE 's/\[(.*)\]/\1/p')" | |
# External GPU (graphics card) | |
IFS=$'|' gpu_ext=( $(echo ${gpu_all[2]}) ) | |
gpu_ext="${gpu_ext[3]} $(echo "${gpu_ext[4]}" | sed -nE 's/\[(.*)\]/\1/p')" | |
# Prefer external gpu | |
gpu="${gpu_ext:-gpu_int}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: this tool has been enhanced a lot and is now available here: https://github.com/SamantazFox/Sam-Shell-Scripts/tree/master/nanofetch