Skip to content

Instantly share code, notes, and snippets.

@espeon
Last active January 28, 2021 18:09
Show Gist options
  • Select an option

  • Save espeon/3e0df4a059d49f0652fe1b004c084298 to your computer and use it in GitHub Desktop.

Select an option

Save espeon/3e0df4a059d49f0652fe1b004c084298 to your computer and use it in GitHub Desktop.
my custom motd on most of my machines. adapted from https://www.reddit.com/r/unixporn/comments/js5jdp/oc_my_fancy_motd_og_unixporn/
#!/bin/bash
# Note(Mike): Avoid using `command` like the plague, prefer $(command)
underline=$( tput smul )
nounderline=$( tput rmul )
bold=$( tput bold )
normal=$( tput sgr0 )
# Note(Mike): Not sure what these do...
# W="\e[0;39m"
# G="\e[1;33m"
# Basic info
HOSTNAME=$( uname -n )
ROOT=$( df -Ph | grep xvda1 | awk '{print $4}' | tr -d '\n' ) # Note(Mike): you should keep the {'s in Awk inside the quotes
KERNEL=$( uname -r )
UPTIME=$( uptime -p | sed -r 's/up\s{0,}//g' )
HOST=$( cat /proc/cpuinfo | grep 'Model' | uniq | sed 's/^.*: //' )
# Get CPU information
# Taken and modified from Dylan Araps's Neofetch
CPU=$(lscpu | grep "Model name:" | sed -r 's/Model name:\s{1,}//g')
SPEED=$(< /sys/devices/system/cpu/cpufreq/policy0/cpuinfo_max_freq) # Note(Mike): You can read a file into a variable with v=$(< file) (this does remove any newlines at the end of the file, if you want to keep them use `read -r -d '' variable < file`)
if [[ $SPEED -lt 1000 ]]; then
SPEED="$SPEED mhz"
else
# Note(Mike): The echo in $(echo $((SPEED / 1000))) is redundant
SPEED=$((SPEED / 1000))
SPEED="$((SPEED / 1000)).$((SPEED % 1000)) ghz"
fi
CORES=$(lscpu | grep "CPU(s):" | sed -r 's/CPU\(s\):\s{1,}//g')
# Note(Mike): in [['s you don't need to quote variables, Bash keeps them intact.
# The same can't be said for single ['s though... One reason among many to
# prefer [[ over [.
# Get CPU temp.
[[ -f $temp_dir ]] && deg="$(($(< "$temp_dir") * 100 / 10000))"
# System load
IFS=" " read USED FREE TOTAL <<<$(free -htm | grep "Mem" | awk '{print $3,$4,$2}')
ISF2=" " read USEDS FREES TOTALS <<<$(free -htm | grep "Swap" | awk '{print $3,$4,$2}')
# Note(Mike): Don't need cat file | command when you can just command < file,
# doing so spins up an unnecessary process (cat)
LOAD1=$( awk '{print $1}' < /proc/loadavg )
LOAD5=$( awk '{print $2}' < /proc/loadavg )
LOAD15=$( awk '{print $3}' < /proc/loadavg )
[ -r /etc/lsb-release ] && . /etc/lsb-release
# Note(Mike): Probably not needed
# OS=$(printf "%s")
# USER=${whoami}
IFS= read -r -d '' motd <<MOTD
_..._ -----------------------------------------------
.' '. _ Welcome to ${bold}$HOSTNAME${normal} running ${bold}$DISTRIB_DESCRIPTION${normal}
/ .-""-\ _/ \
.-| /:. | | | ${underline}Stats: ${nounderline}
| \ |:. /.-'-./ - Host................: $HOST
| .-'-;:__.' =/ - Uptime..............: $UPTIME
.'= *=|(ESA)_.=' - CPU.................: $CPU x$CORES @ $SPEED
/ _ . | ; - CPU usage...........: $LOAD1 (1min), $LOAD5 (5min), $LOAD15 (15min)
;-.-'| \ | - Memory used.........: $USED used | $TOTAL in total
/ | \ _\ _\ - Swap in use.........: $USEDS used | $TOTALS in total
\__/'._;. ==' ==\ -----------------------------------------------
\ \ |
/ / / UNAUTHORIZED ACCESS TO THIS DEVICE IS PROHIBITED
/-._/-._/ You must have explicit, authorized permission to access or configure this device.
\ \`\ \ All activities performed on this device are logged and monitored.
\`-._/._/
MOTD
echo
echo -e "$motd"
# Drives
mountpoint=/dev/mmcblk0p2
barWidth=70
maxDiscUsage=90
clear='\e[39m\e[0m'
dim='\e[2m'
barclear= # Note(Mike): You don't need quotes if the value doesn't have spaces
line=$(df -hl "$mountpoint")
label=$(lsblk -o label "$mountpoint")
usagePercent=$(echo "$line" | tail -n1 | awk '{print $5;}' | sed 's/%//')
usedBarWidth=$(((usagePercent * barWidth) / 100)) # Note(Mike): Don't need dollar sign for variables in arithmetic expansion: $(( foo + bar ))
barContent=
color="\e[33m"
if [[ $usagePercent -ge $maxDiscUsage ]]; then
color="\e[31m"
fi
barContent=$color
for sep in $(seq 1 $usedBarWidth); do
barContent="${barContent}|"
done
barContent=${barContent}${clear}${dim}
for sep in $(seq 1 $((barWidth - usedBarWidth))); do
barContent="${barContent}-"
done
bar=[${barContent}${clear}]
lab=" ${label}" | awk '{if ($1 != "LABEL") printf("%-10s", $1); }' # Note(Mike): Not sure what's happening here lol
echo "${line}" | awk '{if ($1 != "Filesystem") printf(" %s - %s / %s\n", $1, $3, $2); }'
echo -e " ${bar}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment