Refactor scripts to be POSIX compliant

This commit is contained in:
2025-04-23 13:49:43 +01:00
parent 28efced6a3
commit 30ce9b234c
14 changed files with 138 additions and 148 deletions

View File

@@ -1,4 +1,4 @@
#! /bin/bash
#! /bin/sh
RED="\e[31m"
GREEN="\e[32m"
@@ -8,13 +8,13 @@ CYAN="\e[36m"
BOLD="\e[1m"
NC="\e[0m"
BATTERY_INFO=$(upower -i $(upower -e | grep 'BAT'))
BATTERY_INFO=$(upower -i "$(upower -e | grep 'BAT')")
BATTERY_PERCENT=$(echo "$BATTERY_INFO" | grep -oP 'percentage:\s+\K\d+')
BATTERY_STATUS=$(echo "$BATTERY_INFO" | grep -oP 'state:\s+\K\w+')
CHARGE_CYCLES=$(echo "$BATTERY_INFO" | grep -oP 'cycle-count:\s+\K\d+')
WARNING_LEVEL=$(echo "$BATTERY_INFO" | grep -oP 'warning-level:\s+\K\w+')
if [ "$BATTERY_STATUS" == "charging" ] || [ "$BATTERY_STATUS" == "pending" ]; then
if [ "$BATTERY_STATUS" = "charging" ] || [ "$BATTERY_STATUS" = "pending" ]; then
COLOR=$CYAN
elif [ "$BATTERY_PERCENT" -ge 80 ]; then
COLOR=$GREEN
@@ -24,10 +24,10 @@ else
COLOR=$RED
fi
echo -e "${BOLD}Battery: ${COLOR}$BATTERY_PERCENT% ($BATTERY_STATUS)${NC}"
echo "${BOLD}Battery: ${COLOR}$BATTERY_PERCENT% ($BATTERY_STATUS)${NC}"
if [ -n "$CHARGE_CYCLES" ]; then
echo -e "${BOLD}Charge Cycles: ${MAGENTA}$CHARGE_CYCLES${NC}"
echo "${BOLD}Charge Cycles: ${MAGENTA}$CHARGE_CYCLES${NC}"
fi
if [ "$WARNING_LEVEL" != "none" ]; then
echo -e "${BOLD}Warning Level: ${RED}$WARNING_LEVEL${NC}"
echo "${BOLD}Warning Level: ${RED}$WARNING_LEVEL${NC}"
fi

View File

@@ -1,20 +1,20 @@
#! /bin/bash
#! /bin/sh
YELLOW="\e[33m"
NC="\e[0m"
echo -e "\n${YELLOW}Downloading APT dependencies...${NC}\n"
echo "\n${YELLOW}Downloading APT dependencies...${NC}\n"
sudo apt install build-essential git -y
sudo apt build-dep linux -y
echo -e "\n${YELLOW}Downloading kernel source...${NC}\n"
mkdir linux-parent && cd linux-parent
echo "\n${YELLOW}Downloading kernel source...${NC}\n"
mkdir linux-parent && cd linux-parent || exit 1
git clone --depth 1 https://github.com/torvalds/linux
cd linux
cd linux || exit 1
cp /boot/config-$(uname -r) .config # Copy current kernel config
make nconfig # Edit the current kernel configuration
diff /boot/config-$(uname -r) .config # Check your changes
cp /boot/config-"$(uname -r)" .config # Copy current kernel config
make nconfig # Edit the current kernel configuration
diff /boot/config-"$(uname -r)" .config # Check your changes
# Do not include debugging symbols. Alternatively, use `strip` to remove them. (these configs are working as of 6.14)
scripts/config --undefine GDB_SCRIPTS
@@ -26,14 +26,14 @@ scripts/config --set-val DEBUG_INFO_NONE y
scripts/config --set-val DEBUG_INFO_DWARF5 n
scripts/config --disable DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT
echo -e "\n${YELLOW}Compiling the kernel...${NC}\n"
make -j$(nproc) deb-pkg LOCALVERSION=-custom
echo "\n${YELLOW}Compiling the kernel...${NC}\n"
make -j"$(nproc)" deb-pkg LOCALVERSION=-custom
echo -e "\n${YELLOW}Installing the generated dpkg packages...${NC}\n"
echo "\n${YELLOW}Installing the generated dpkg packages...${NC}\n"
sudo dpkg -i ../linux-headers*-custom*.deb
sudo dpkg -i ../linux-image*-custom*.deb
echo -e "\n${YELLOW}Cleaning up...${NC}\n"
echo "\n${YELLOW}Cleaning up...${NC}\n"
cd ../..
rm -rf linux-parent

View File

@@ -1,3 +1,5 @@
#! /bin/bash
for i in {0..255}; do
printf "\x1b[48;5;%sm%3d\e[0m " "$i" "$i"
if ((i == 15)) || ((i > 15)) && (((i - 15) % 6 == 0)); then

View File

@@ -1,4 +1,4 @@
#! /bin/bash
#! /bin/sh
extract() {
if [ -f "$1" ]; then

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
# Basic System Information Script
RED="\e[31m"
@@ -10,50 +10,52 @@ BOLD="\e[1m"
NC="\e[0m"
# User and host info
echo -e "${BOLD}${MAGENTA}${USER}@$(hostname)${NC}"
echo "${BOLD}${MAGENTA}${USER}@$(hostname)${NC}"
echo "---------"
# OS and architecture
OS=$(grep '^PRETTY_NAME=' /etc/os-release | cut -d= -f2 | tr -d '"')
ARCH=$(uname -m)
echo -e "${CYAN}OS:${NC} $OS $ARCH"
echo "${CYAN}OS:${NC} $OS $ARCH"
# Host Model
HOST_MODEL=$(cat /sys/class/dmi/id/product_name 2>/dev/null)
HOST_VERSION=$(cat /sys/class/dmi/id/product_version 2>/dev/null)
echo -e "${CYAN}Host:${NC} ${HOST_VERSION} (${HOST_MODEL})"
echo "${CYAN}Host:${NC} ${HOST_VERSION} (${HOST_MODEL})"
# Kernel version
echo -e "${CYAN}Kernel:${NC} Linux $(uname -r)"
echo "${CYAN}Kernel:${NC} Linux $(uname -r)"
# Uptime
UPTIME=$(uptime -p | sed 's/up //')
echo -e "${CYAN}Uptime:${NC} $UPTIME"
echo "${CYAN}Uptime:${NC} $UPTIME"
# Package counts: dpkg and flatpak
PKG_COUNT=$(dpkg-query -f '${binary:Package}\n' -W 2>/dev/null | wc -l)
FLAT_COUNT=$(flatpak list 2>/dev/null | wc -l)
echo -e "${CYAN}Packages:${NC} ${PKG_COUNT} (dpkg), ${FLAT_COUNT} (flatpak)"
echo "${CYAN}Packages:${NC} ${PKG_COUNT} (dpkg), ${FLAT_COUNT} (flatpak)"
# Shell and version
BASH_VER=$(bash --version | head -n1 | awk '{print $4}' | cut -d'(' -f1)
echo -e "${CYAN}Shell:${NC} bash ${BASH_VER}"
echo "${CYAN}Shell:${NC} bash ${BASH_VER}"
# Desktop Environment
DE=${XDG_CURRENT_DESKTOP:-"Unknown"}
echo -e "${CYAN}DE:${NC} $DE"
echo "${CYAN}DE:${NC} $DE"
# CPU model from /proc/cpuinfo
CPU=$(awk -F: '/model name/ {print $2; exit}' /proc/cpuinfo | sed 's/^[ \t]*//')
echo -e "${CYAN}CPU:${NC} $CPU"
echo "${CYAN}CPU:${NC} $CPU"
# GPU info
GPU=$(lspci | grep -i 'vga\|3d' | head -n1 | cut -d: -f3 | sed 's/^[ \t]*//')
echo -e "${CYAN}GPU:${NC} $GPU"
echo "${CYAN}GPU:${NC} $GPU"
# Memory usage
mem_info=$(free | awk '/Mem:/ {print $2, $3}')
read total used <<<"$mem_info"
set -- $mem_info
total=$1
used=$2
percent=$(awk "BEGIN {printf \"%.0f\", ($used/$total)*100}")
if [ "$percent" -le 60 ]; then
@@ -66,7 +68,7 @@ fi
mem_total=$(free -h | awk '/Mem:/ {print $2}')
mem_used=$(free -h | awk '/Mem:/ {print $3}')
echo -e "${CYAN}Memory:${NC} ${mem_used} / ${mem_total} (${percent_color}${percent}%${NC})"
echo "${CYAN}Memory:${NC} ${mem_used} / ${mem_total} (${percent_color}${percent}%${NC})"
# Swap usage
swap_used=$(free -h | awk '/Swap/ {print $3}')
@@ -81,13 +83,13 @@ else
swap_perc_color=$RED
fi
echo -e "${CYAN}Swap:${NC} ${swap_used} / ${swap_total} (${swap_perc_color}${swap_perc}%${NC})"
echo "${CYAN}Swap:${NC} ${swap_used} / ${swap_total} (${swap_perc_color}${swap_perc}%${NC})"
# Root disk usage
root_line=$(df -h / | awk 'NR==2')
root_used=$(echo $root_line | awk '{print $3}')
root_total=$(echo $root_line | awk '{print $2}')
root_percent=$(echo $root_line | awk '{gsub("%","",$5); print $5}')
root_used=$(echo "$root_line" | awk '{print $3}')
root_total=$(echo "$root_line" | awk '{print $2}')
root_percent=$(echo "$root_line" | awk '{gsub("%","",$5); print $5}')
if [ "$root_percent" -le 60 ]; then
root_color=$GREEN
elif [ "$root_percent" -le 80 ]; then
@@ -95,14 +97,14 @@ elif [ "$root_percent" -le 80 ]; then
else
root_color=$RED
fi
echo -e "${CYAN}Disk (/):${NC} ${root_used} / ${root_total} (${root_color}${root_percent}%${NC})"
echo "${CYAN}Disk (/):${NC} ${root_used} / ${root_total} (${root_color}${root_percent}%${NC})"
# /home disk usage
home_line=$(df -h /home 2>/dev/null | awk 'NR==2')
if [ -n "$home_line" ]; then
home_used=$(echo $home_line | awk '{print $3}')
home_total=$(echo $home_line | awk '{print $2}')
home_percent=$(echo $home_line | awk '{gsub("%","",$5); print $5}')
home_used=$(echo "$home_line" | awk '{print $3}')
home_total=$(echo "$home_line" | awk '{print $2}')
home_percent=$(echo "$home_line" | awk '{gsub("%","",$5); print $5}')
if [ "$home_percent" -le 60 ]; then
home_color=$GREEN
elif [ "$home_percent" -le 80 ]; then
@@ -110,7 +112,7 @@ if [ -n "$home_line" ]; then
else
home_color=$RED
fi
echo -e "${CYAN}Disk (/home):${NC} ${home_used} / ${home_total} (${home_color}${home_percent}%${NC})"
echo "${CYAN}Disk (/home):${NC} ${home_used} / ${home_total} (${home_color}${home_percent}%${NC})"
fi
# Determine the primary network interface used for internet access
@@ -118,11 +120,11 @@ PRIMARY_IFACE=$(ip route get 8.8.8.8 2>/dev/null | awk '{print $5; exit}')
if [ -n "$PRIMARY_IFACE" ]; then
LOCAL_IP=$(ip addr show "$PRIMARY_IFACE" | awk '/inet / {print $2}' | cut -d'/' -f1)
echo -e "${CYAN}Local IP ($PRIMARY_IFACE):${NC} ${LOCAL_IP}"
echo "${CYAN}Local IP ($PRIMARY_IFACE):${NC} ${LOCAL_IP}"
else
echo -e "${RED}Local IP:${NC} Unable to determine primary network interface."
echo "${RED}Local IP:${NC} Unable to determine primary network interface."
fi
# Current locale setting
LOCALE=$(locale | awk -F= '/^LANG=/{print $2; exit}')
echo -e "${CYAN}Locale:${NC} $LOCALE"
echo "${CYAN}Locale:${NC} $LOCALE"

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
RED="\e[31m"
GREEN="\e[32m"
@@ -6,12 +6,14 @@ YELLOW="\e[33m"
NC="\e[0m"
mem_info=$(free | awk '/Mem:/ {print $2, $3}')
read total used <<<"$mem_info"
set -- $mem_info
total=$1
used=$2
percent=$(awk "BEGIN {printf \"%.0f\", ($used/$total)*100}")
mem_total=$(free -h | awk '/Mem:/ {print $2}')
mem_used=$(free -h | awk '/Mem:/ {print $3}')
if [[ -t 1 ]]; then
if [ -t 1 ]; then
if [ "$percent" -le 60 ]; then
percent_color=$GREEN
elif [ "$percent" -le 80 ]; then
@@ -19,7 +21,7 @@ if [[ -t 1 ]]; then
else
percent_color=$RED
fi
echo -e " ${mem_used} / ${mem_total} (${percent_color}${percent}%${NC})"
echo " ${mem_used} / ${mem_total} (${percent_color}${percent}%${NC})"
else
echo "${mem_used} ${mem_total} ${percent}"
fi

View File

@@ -1,4 +1,4 @@
#! /bin/bash
#! /bin/sh
RED="\e[31m"
GREEN="\e[32m"
@@ -6,11 +6,11 @@ YELLOW="\e[33m"
CYAN="\e[36m"
NC="\e[0m"
trap "echo -e '${CYAN}System Monitor interrupted.${NC}'; exit 1" SIGINT SIGTERM
trap "echo '${CYAN}System Monitor interrupted.${NC}'; exit 1" INT TERM
for i in {0..3}; do
echo
done
echo
echo
echo
# Cache in tmpfs to improve speed and reduce SSD load
cache=/tmp/cpu-script-cache
@@ -48,7 +48,9 @@ while true; do
# MEMORY
mem_info=$(free | awk '/Mem:/ {print $2, $3}')
read total used <<<"$mem_info"
set -- $mem_info
total=$1
used=$2
percent=$(awk "BEGIN {printf \"%.0f\", ($used/$total)*100}")
mem_total=$(free -h | awk '/Mem:/ {print $2}')
@@ -62,11 +64,11 @@ while true; do
mem_color=$RED
fi
echo -ne "\033[4A"
echo -e "============================="
echo -e " ${cpu_color}${usage}%${NC} "
echo -e " ${mem_used} / ${mem_total} (${mem_color}${percent}%${NC}) "
echo -e "============================="
printf "\033[4A"
echo "============================="
echo " ${cpu_color}${usage}%${NC} "
echo " ${mem_used} / ${mem_total} (${mem_color}${percent}%${NC}) "
echo "============================="
echo "$cpu_stats" >"$cache"
sleep 2

View File

@@ -1,17 +0,0 @@
oneline() {
# Print a command's output as a single line only.
# Example usage: for f in 'first line' 'second line' '3rd line'; do echo "$f"; sleep 1; done | oneline
local ws
while IFS= read -r line; do
if ((${#line} >= $COLUMNS)); then
# Moving cursor back to the front of the line so user input doesn't force wrapping
printf '\r%s\r' "${line:0:$COLUMNS}"
else
ws=$(($COLUMNS - ${#line}))
# by writing each line twice, we move the cursor back to position
# thus: LF, content, whitespace, LF, content
printf '\r%s%*s\r%s' "$line" "$ws" " " "$line"
fi
done
echo
}

View File

@@ -1,3 +1,5 @@
#! /bin/bash
#!/bin/bash
paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) | column -s $'\t' -t | sed 's/\(.\)..$/.\1°C/'
paste <(cat /sys/class/thermal/thermal_zone*/type) <(cat /sys/class/thermal/thermal_zone*/temp) |
column -s "$(printf '\t')" -t |
sed 's/\(.\)..$/.\1°C/'

View File

@@ -1,4 +1,4 @@
#! /bin/bash
#! /bin/sh
YELLOW="\e[33m"
NC="\e[0m"

View File

@@ -18,7 +18,7 @@ split() {
vol="$(printf "%.0f" "$(split "$vol" ".")")"
case 1 in
case $1 in
$((vol >= 70))) icon=" " ;;
$((vol >= 30))) icon=" " ;;
$((vol >= 1))) icon=" " ;;