Reorga of functions

moved all functions into files
Redid RAM to be more useable
This commit is contained in:
2026-07-14 16:45:44 +02:00
parent 9039344de5
commit 925c624351
9 changed files with 230 additions and 217 deletions

46
RAM.sh Executable file
View File

@@ -0,0 +1,46 @@
#!/usr/bin/env bash
function RAM(){
local OUT_FILE=$1
# Read RAM information
ram_info=$(sudo dmidecode -t memory)
# Get memory type
ram_type=$(echo "$ram_info" | awk -F': ' '/^[[:space:]]*Type:/ && $2 !~ /Unknown/ {print $2; exit}')
# Get module sizes
modules=$(echo "$ram_info" | awk '
/^[[:space:]]*Size:/ {
if ($0 !~ /No Module Installed/ && $2 ~ /^[0-9]+$/)
print $2
}
')
# Count empty slots
empty_slots=$(echo "$ram_info" | awk '
/^[[:space:]]*Size:/ {
if ($0 ~ /No Module Installed/)
count++
}
END {
print count+0
}
')
# Calculate total RAM and format modules
total=0
sizes=""
for size in $modules; do
total=$((total + size))
if [ -z "$sizes" ]; then
sizes="${size} GB"
else
sizes="${sizes} + ${size} GB"
fi
done
# Output
if [ -z "$modules" ]; then
echo -e "RAM: No modules detected - ${empty_slots} empty slots ${ram_type}" >>"$OUT_FILE"
else
echo -e "RAM: ${total} GB - ${sizes} ${ram_type} - ${empty_slots} empty slots" >>"$OUT_FILE"
fi
}

View File

@@ -1,25 +1,30 @@
#!/bin/bash
function BATTERY(){
local OUT_FILE=$1
for b in /sys/class/power_supply/BAT*; do
[ -d "$b" ] && BATTERY="$b" && break
done
if [ -z "$BATTERY" ]; then
echo "Kein akku gefunden"
echo -e "Kein akku gefunden">>"$OUT_FILE"
return 1
fi
if [ -f "$BATTERY/capacity" ]; then
CAPACITY=$(cat "$BATTERY/capacity")
else
echo "Akku kann nicht gelesen werden"
echo -e "Akku kann nicht gelesen werden">>"$OUT_FILE"
return 1
fi
if [ "$CAPACITY" -gt 0 ]; then
echo "Akku bei $CAPACITY % "
echo -e "Akku bei $CAPACITY % ">>"$OUT_FILE"
return 0
else
echo "Akkustand 0%, prüfen"
echo -e "Akkustand 0%, prüfen">>"$OUT_FILE"
return 1
fi
}

View File

@@ -1,13 +1,14 @@
#!/bin/bash
function CAMERA(){
local OUT_FILE=$1
snapshot >/dev/null 2>&1 & disown
read -p "Funktioniert Kamera? Snapshot öffnet sich - (y/n): " answer
#output
if [[ "$answer" == [Yy] ]]; then
return 0
elif [[ "$answer" == [Nn] ]]; then
return 1
echo -e "Kamera:\t Ja" >>"$OUT_FILE"
else
return 1
echo -e "Kamera:\t Nein" >>"$OUT_FILE"
fi
}

8
lan.sh
View File

@@ -1,10 +1,8 @@
#!/bin/bash
function LAN_CABLE(){
for iface in /sys/class/net/*; do
iface_name=$(basename "$iface")
#echo "${iface_name:0:2}"
# Skip loopback
if [[ "$iface_name" == "lo" ]]; then
continue
fi
@@ -13,7 +11,7 @@ for iface in /sys/class/net/*; do
continue
fi
# Check if carrier file exists (only for physical interfaces)
if [[ -f "$iface/carrier" ]]; then
carrier=$(cat "$iface/carrier")
@@ -24,6 +22,6 @@ for iface in /sys/class/net/*; do
fi
fi
done
echo "LAN cable is NOT connected"
return 1
}

13
model.sh Normal file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
function MODEL(){
local OUT_FILE=$1
MODEL=$(cat /sys/class/dmi/id/product_name)
echo -e "Model:\t$MODEL" >>"$OUT_FILE"
sudo hostnamectl set-hostname "csr-$MODEL"
}
function CPU(){
local OUT_FILE=$1
CPUModel=$(cat /proc/cpuinfo | grep "model name" | head -1 | cut -d: -f2- | sed 's/^[[:space:]]*//' )
echo -e "CPU:\t$CPUModel" >>"$OUT_FILE"
}

View File

@@ -1,15 +1,16 @@
#!/bin/bash
function SCREEN(){
local OUT_FILE=$1
# Get primary display name
display=$(xrandr --query | awk '/ connected primary/{print $1}')
# If no primary, use first connected
if [ -z "$display" ]; then
display=$(xrandr --query | awk '/ connected/{print $1; exit}')
fi
if [ -z "$display" ]; then
echo "No connected display found."
echo -e "No connected display found." >>"$OUT_FILE"
exit 1
fi
@@ -20,14 +21,18 @@ dim_line=$(xrandr --query | grep "^$display connected")
width_mm=$(echo "$dim_line" | sed -n 's/.* \([0-9]\+\)mm x \([0-9]\+\)mm.*/\1/p')
height_mm=$(echo "$dim_line" | sed -n 's/.* \([0-9]\+\)mm x \([0-9]\+\)mm.*/\2/p')
if [ -z "$width_mm" ] || [ -z "$height_mm" ]; then
echo "Could not determine physical screen size."
echo -e "Could not determine physical screen size." >>"$OUT_FILE"
exit 1
fi
# Calculate diagonal in mm
diagonal_mm=$(echo "scale=4; sqrt($width_mm^2 + $height_mm^2)" | bc -l)
# Convert to inches
diagonal_in=$(echo "scale=2; $diagonal_mm / 25.4" | bc -l)
return ${diagonal_in%.*}
echo -e "Screen:\t ${diagonal_in%.*}" >>"$OUT_FILE"
return 0
}

View File

@@ -1,13 +1,13 @@
#!/bin/bash
function SPEAKER(){
local OUT_FILE=$1
audacious ./stereo_test.mp3 >/dev/null 2>&1 & disown
read -p "Funktioniert Sound? Audacios öffnet sich - (y/n): " answer
if [[ "$answer" == [Yy] ]]; then
return 0
elif [[ "$answer" == [Nn] ]]; then
return 1
echo -e "Speaker:\t Ja" >>"$OUT_FILE"
else
return 1
echo -e "Speaker:\t Nein" >>"$OUT_FILE"
fi
}

90
test.sh
View File

@@ -5,87 +5,31 @@ DIR="${BASH_SOURCE%/*}"
DESKTOP_PATH=$(xdg-user-dir DESKTOP)
OUT="$DESKTOP_PATH/device_info.txt"
source drives.sh
source RAM.sh
source camera.sh
source speaker.sh
source wifi.sh
source lan.sh
source screen.sh
source battery.sh
source model.sh
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
get_cpu_model () {
CPUModel=$(cat /proc/cpuinfo | grep "model name" | head -1 | cut -d: -f2- | sed 's/^[[:space:]]*//' )
}
get_model () {
MODEL=$(cat /sys/class/dmi/id/product_name)
}
LAN_CABLE
get_RAM () {
LANGUAGE="${LC_MESSAGES:-$LANG}"
if [[ "$LANGUAGE" == de* ]]; then
TOTAL_MEM=$(free -g | grep Speicher: | awk '{print $2}')
elif [[ "$LANGUAGE" == en* ]]; then
TOTAL_MEM=$(free -g | awk '/^Mem:/ {print $2}')
fi
}
. "$DIR/lan.sh"
get_cpu_model
get_model
get_RAM
sudo hostnamectl set-hostname "csr-$MODEL"
clear
echo -e "$(date -I)" >"$OUT"
echo -e "CPU:\t$CPUModel" >>"$OUT"
echo -e "Model:\t$MODEL" >>"$OUT"
echo -e "RAM:\t$TOTAL_MEM GB" >> "$OUT"
. "$DIR/camera.sh"
retval=$?
if [ "$retval" == 0 ]
then
echo -e "Kamera:\t Ja" >>"$OUT"
else
echo -e "Kamera:\t Nein" >>"$OUT"
fi
. "$DIR/speaker.sh"
retval=$?
if [ "$retval" == 0 ]
then
echo -e "Speaker:\t Ja" >>"$OUT"
else
echo -e "Speaker:\t Nein" >>"$OUT"
fi
. "$DIR/wifi.sh"
retval=$?
if [ "$retval" == 0 ] ; then
echo -e "WIFI:\t Ja" >>"$OUT"
else
echo -e "WIFI:\t Nein" >>"$OUT"
fi
. "$DIR/screen.sh"
in_retval=$?
if [ "$in_retval" != 0 ]
then
echo -e "Screen:\t $in_retval" >>"$OUT"
else
echo -e "Screen check failed" >>"$OUT"
fi
#clear
. "$DIR/battery.sh"
retval=$?
if [ "$retval" == 0 ];then
echo -e "AKKU:\t Ja" >>"$OUT"
else
echo -e "AKKU:\t Nein" >>"$OUT"
fi
MODEL $OUT
CPU $OUT
CAMERA $OUT
SPEAKER $OUT
WIFI $OUT
SCREEN $OUT
BATTERY $OUT
drives $OUT
RAM $OUT
cat "$OUT"

23
wifi.sh
View File

@@ -1,26 +1,25 @@
#!/bin/bash
function WIFI(){
local OUT_FILE=$1
SSID="CSR_Gastzugang"
PASSWORD="CSR_2021"
nmcli connection delete $SSID #delete network to safely test
#delete network to safely test
nmcli connection delete $SSID
if [[ -z "$SSID" || -z "$PASSWORD" ]]; then
echo "Usage: $0 <SSID> <PASSWORD>"
echo -e "WIFI:\t Nein" >>"$OUT_FILE"
return 1
fi
echo "Verbinde: $SSID ..."
nmcli dev wifi connect "$SSID" password "$PASSWORD" >/dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "Verbinding fehlgeschlagen $SSID"
echo -e "WIFI:\t Nein" >>"$OUT_FILE"
return 2
fi
echo "Verbindsaufbau..."
# Wait up to 15 seconds for connection
for i in {1..15}; do
STATE=$(nmcli -t -f WIFI g)
@@ -33,9 +32,9 @@ for i in {1..15}; do
fi
sleep 1
done
if [[ "$CONN" != "$SSID" ]]; then
echo "Verbindingsfehler"
echo -e "WIFI:\t Nein" >>"$OUT_FILE"
return 3
fi
@@ -43,23 +42,25 @@ echo "Teste (ping 8.8.8.8)..."
ping -c 3 8.8.8.8 >/dev/null 2>&1
PING_STATUS=$?
if [[ $PING_STATUS -ne 0 ]]; then
echo "Kein Internet"
echo -e "WIFI:\t Nein" >>"$OUT_FILE"
return 4
fi
echo "Ping Erfolg"
echo "Teste HTTP..."
curl -s --max-time 5 https://google.com >/dev/null 2>&1
CURL_STATUS=$?
if [[ $CURL_STATUS -ne 0 ]]; then
echo "HTTP fehlgeschlagen"
echo -e "WIFI:\t Nein" >>"$OUT_FILE"
return 5
fi
echo "Internet funktioniert!"
echo -e "WIFI:\t Ja" >>"$OUT_FILE"
return 0
}