I would like to do a script that does “exact” same thing like this script :
#!/usr/bin/env bash
# Dependencies: bash>=3.2, coreutils, file, gawk
# Makes the script more portable
readonly DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Optional icon to display before the text
# Insert the absolute path of the icon
# Recommended size is 24x24 px
readonly ICON="${DIR}/icons/memory/memory.png"
# Calculate RAM values
readonly TOTAL=$(free -b | awk '/^[Mm]em/{$2 = $2 / 1073741824; printf "%.2f", $2}')
readonly USED=$(free -b | awk '/^[Mm]em/{$3 = $3 / 1073741824; printf "%.2f", $3}')
readonly FREE=$(free -b | awk '/^[Mm]em/{$4 = $4 / 1073741824; printf "%.2f", $4}')
readonly SHARED=$(free -b | awk '/^[Mm]em/{$5 = $5 / 1073741824; printf "%.2f", $5}')
readonly CACHED=$(free -b | awk '/^[Mm]em/{$6 = $6 / 1073741824; printf "%.2f", $6}')
readonly AVAILABLE=$(free -b | awk '/^[Mm]em/{$7 = $7 / 1073741824; printf "%.2f", $7}')
# Swap Values
readonly SWP_TOTAL=$(free -b | awk '/^[Ss]wap/{$2 = $2 / 1073741824; printf "%.2f", $2}')
readonly SWP_USED=$(free -b | awk '/^[Ss]wap/{$3 = $3 / 1073741824; printf "%.2f", $3}')
readonly SWP_FREE=$(free -b | awk '/^[Ss]wap/{$4 = $4 / 1073741824; printf "%.2f", $4}')
# Panel
if [[ $(file -b "${ICON}") =~ PNG|SVG ]]; then
INFO="<img>${ICON}</img>"
if hash xfce4-taskmanager &> /dev/null; then
INFO+="<click>xfce4-taskmanager</click>"
fi
INFO+="<txt>"
else
INFO="<txt>"
fi
INFO+="MEM: ${USED} GB"
INFO+="</txt>"
# Tooltip
MORE_INFO="<tool>"
MORE_INFO+="┌ RAM\n"
MORE_INFO+="├─ Used\t\t${USED} GB\n"
MORE_INFO+="├─ Free\t\t${FREE} GB\n"
MORE_INFO+="├─ Shared\t${SHARED} GB\n"
MORE_INFO+="├─ Cache\t${CACHED} GB\n"
MORE_INFO+="└─ Total\t\t${TOTAL} GB"
MORE_INFO+="\n\n"
MORE_INFO+="┌ SWAP\n"
MORE_INFO+="├─ Used\t\t${SWP_USED} GB\n"
MORE_INFO+="├─ Free\t\t${SWP_FREE} GB\n"
MORE_INFO+="└─ Total\t\t${SWP_TOTAL} GB"
MORE_INFO+="</tool>"
# Panel Print
echo -e "${INFO}"
# Tooltip Print
echo -e "${MORE_INFO}"
BUT i want it to list my cpu cores, the usage of it and the temperature:
print the overal cpu usage to the panel, but if you move the coursor to the “script” it gives a list like this :
cpu1 12% 34
cpu2 22% 56
etc.
Is it possible? How could i write it? I have never wrote a single bash script or program.
This is a rabbit hole you are going to embark on, bash scripting isn’t terribly hard, but the one you have here is rather involved.
My recommendation would be for you to look up the free online version of the linux command line by William Shotts a book that explained everything needed in a fashion, that I could understand it. It has a significant section about bash scripts, with practical examples.
https://linuxcommand.org/tlcl.php
The other thing you could do, and that is the hard way i wouldn’t recommend, is trying to understand the code above line for line. Something that also will be easier after reading the aforementioned material. You can then spot what to modify. For us noobs bash scripting is frequently trial and error.
2 Likes
oguere
20 January 2022 02:47
3
It’s possible, you need to read the /proc/stat file instead of using the free
command :
## here is the content of this file :
~ $ cat /proc/stat
cpu 12230489 1135 3229211 26918076 24893 633503 256670 0 0 0
cpu0 1794000 258 465993 24412248 24099 454648 138188 0 0 0
cpu1 1164027 191 287873 370621 78 24719 26944 0 0 0
cpu2 1975394 125 502585 341608 165 38993 21677 0 0 0
cpu3 1137165 84 309265 370367 70 20957 13507 0 0 0
cpu4 1944100 218 514277 339446 101 30438 17813 0 0 0
cpu5 1195013 59 311545 370269 93 16994 10280 0 0 0
cpu6 1980190 113 525918 340184 232 29512 18067 0 0 0
cpu7 1040597 84 311751 373329 51 17238 10190 0 0 0
intr 1310896691 38 146626 0 0 0 0 136113222 7808398 1 1819 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 135 0 55 0 0 2304750 0 0 0 0 0 0 0 0 0 0 365010 0 0 0 0 0 0 0 7806976 0 0 0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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 2494472606
btime 1641921712
processes 249304
procs_running 2
procs_blocked 0
softirq 326730803 197206 22952558 570 16088534 2192441 245 178530 166232424 440562 118447733
so basically you will want to store this output into a temp file with mktmp
and use awk
or cut
on it. At least this is how I would start.
1 Like