-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbfetch
More file actions
executable file
·182 lines (161 loc) · 5.17 KB
/
bfetch
File metadata and controls
executable file
·182 lines (161 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env bash
#
# Yet another fetch script in bash
#
# Author: Justus0405
# Date: 11.07.2024
# License: MIT
export scriptVersion="1.2"
### COLOR CODES ###
export black="\e[1;30m"
export red="\e[1;31m"
export green="\e[1;32m"
export yellow="\e[1;33m"
export blue="\e[1;34m"
export purple="\e[1;35m"
export cyan="\e[1;36m"
export lightGray="\e[1;37m"
export gray="\e[1;90m"
export lightRed="\e[1;91m"
export lightGreen="\e[1;92m"
export lightYellow="\e[1;93m"
export lightBlue="\e[1;94m"
export lightPurple="\e[1;95m"
export lightCyan="\e[1;96m"
export white="\e[1;97m"
export bold="\e[1m"
export faint="\e[2m"
export italic="\e[3m"
export underlined="\e[4m"
export blinking="\e[5m"
export reset="\e[0m"
### FUNCTIONS ###
getArguments() {
case "$1" in
"-s" | "--system")
printSystem
;;
"-t" | "--temps")
printTemperatures
;;
"-h" | "--help")
printHelp
;;
"-v" | "--version")
printVersion
;;
"")
printSystem
;;
*)
echo -e "Error: Unrecognized option '$1'. Use $(basename "$0") help"
exit 1
;;
esac
}
### System Handling ###
printSystem() {
### General System Info ###
showUser=$(whoami)
showHostname=$(cat /etc/hostname)
showKernel=$(uname -r)
showUptime=$(uptime -p | sed 's/up //')
# Reads the age of the file machine-id to determine the OS age
if [[ -f "/etc/machine-id" ]]; then
showAge=$((($(date +%s) - $(date -r "/etc/machine-id" +%s)) / 86400))
else
showAge="Error: File not found"
fi
### Usage Info ###
# Show ram usage in mb, cut everything except line 2, calculate used percentage
showRamUsage=$(free -m | awk 'NR==2{used=$3; total=$2; printf "%dmb / %dmb (%.0f%%)", used, total, used/total*100}')
# Show total disk usage in gb, only use line total , print values
showDiskUsage=$(df -h --total | awk '/total/ {printf "%s / %s (%s)", $3, $2, $5}')
### Network Info ###
# Tries to ping google dns server, if successful return Online, if not return Offline
showOnlineStatus=$(if ping -c 1 8.8.8.8 &>/dev/null; then echo -e "${green}Online${reset}"; else echo -e "${red}Offline${reset}"; fi)
# Show every IPv4 adress, filter only for the numbers, remove loopback adress, show remaining adress
showLocalIp=$(ip -4 addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1' | head -n 1)
echo -e ""
echo -e "\t╭─────────────╮"
echo -e "\t│ ${red}User${reset} │ ${showUser}"
echo -e "\t│ ${yellow}Kernel${reset} │ ${showKernel}"
echo -e "\t│ ${green}Uptime${reset} │ ${showUptime}"
echo -e "\t│ ${cyan}Age${reset} │ ${showAge} days"
echo -e "\t│ ${blue}Ram${reset} │ ${showRamUsage}"
echo -e "\t│ ${purple}Disk${reset} │ ${showDiskUsage}"
echo -e "\t├─────────────┤"
echo -e "\t│ ${red}Hostname${reset} │ ${showHostname}"
echo -e "\t│ ${yellow}Internet${reset} │ ${showOnlineStatus}"
echo -e "\t│ ${green}Local IP${reset} │ ${showLocalIp}"
echo -e "\t├─────────────┤"
echo -e "\t│ colors │ ${white} ${red} ${yellow} ${green} ${cyan} ${blue} ${purple} ${black} ${reset}"
echo -e "\t╰─────────────╯"
echo -e ""
}
### Temperature Handling ###
findTempPath() {
local potentialPaths=("$@")
for path in "${potentialPaths[@]}"; do
if [ -f "${path}" ] && [ -s "${path}" ]; then
echo "${path}"
return 0
fi
done
echo ""
}
printTemperatures() {
echo -e ""
echo -e "\t> Temperatures"
potentialPathsZone0=(
"/sys/class/thermal/thermal_zone0/temp"
"/sys/class/hwmon/hwmon0/temp1_input"
"/sys/class/hwmon/hwmon1/temp1_input"
)
potentialPathsZone1=(
"/sys/class/thermal/thermal_zone1/temp"
"/sys/class/hwmon/hwmon0/temp2_input"
"/sys/class/hwmon/hwmon1/temp2_input"
)
thermalZone0Path=$(findTempPath "${potentialPathsZone0[@]}")
thermalZone1Path=$(findTempPath "${potentialPathsZone1[@]}")
if [[ -n "${thermalZone0Path}" ]]; then
temp0=$(cat "${thermalZone0Path}" 2>/dev/null)
if [[ ${temp0} =~ ^[0-9]+$ ]]; then
thermalZone0=$(cat /sys/class/thermal/thermal_zone0/type 2>/dev/null || echo "Zone 0")
thermalZone0Temp=$((temp0 / 1000))
echo -e "\t${red}${thermalZone0}${reset}: ${cyan}${thermalZone0Temp}°C${reset}"
fi
fi
if [[ -n "${thermalZone1Path}" ]]; then
temp1=$(cat "${thermalZone1Path}" 2>/dev/null)
if [[ ${temp1} =~ ^[0-9]+$ ]]; then
thermalZone1=$(cat /sys/class/thermal/thermal_zone1/type 2>/dev/null || echo "Zone 1")
thermalZone1Temp=$((temp1 / 1000))
echo -e "\t${red}${thermalZone1}${reset}: ${cyan}${thermalZone1Temp}°C${reset}"
fi
fi
if [[ -z "${thermalZone0Path}" ]] && [[ -z "${thermalZone1Path}" ]]; then
echo -e "\t${red}ERROR:${reset} No valid temperature sensor found"
exit 1
fi
echo -e ""
}
printHelp() {
echo -e "usage: $(basename "$0") [...]"
echo -e "arguments:"
echo -e "\t -s | --system"
echo -e "\t -t | --temps"
echo -e "\t -h | --help"
echo -e "\t -v | --version"
echo -e ""
exit 0
}
printVersion() {
echo -e " $(basename "$0") v${scriptVersion} - GNU bash, version 5.3"
echo -e " Copyright (C) 2025-present Justus0405"
echo -e ""
exit 0
}
### PROGRAM START ###
getArguments "$@"