#!/bin/bash ###################### ## GLOBAL VARIABLES ## ###################### ## Temporary files ## dir="/tmp/" file_volume="$dir.volume.txt" file_battery="$dir.battery.txt" file_brightness="$dir.brightness.txt" file_date="$dir.date.txt" file_status="$dir.status.txt" file_weather="$dir.weather.txt" file_ssh="$dir.ssh.txt" file_network="$dir.network.txt" file_final="$dir.final" ## Other shared values ## actual_brightness="/sys/class/backlight/amdgpu_bl0/brightness" #################### ## MAIN FUNCTIONS ## #################### ## Start it all ## function start-loop { # if the argument is empty then run the update-weather (otherwise takes long time to update if and shows latesrt value only.. =[) set-bar function if [ -z "$1" ] then update-weather set-bar # else run given control function else $1 $2 fi } ## Main update string ## function update-all { # declare the output string from each of the functions final_volume=$(cat "$file_volume") final_battery=$(cat "$file_battery") final_brightness=$(cat "$file_brightness") final_date=$(cat "$file_date") final_status=$(cat "$file_status") final_weather=$(cat "$file_weather") final_ssh=$(cat "$file_ssh") final_network=$(cat "$file_network") # generate the final output string to be given to the bar final_string="$final_network  $final_ssh  $final_status  $final_battery  $final_brightness  $final_volume  $final_weather  $final_date " last_string=$(cat "$file_final") # check if any changes between the last string and the current bar string if [ "$final_string" == "$last_string" ] then exit 0; else # if needed update final bar string xsetroot -name "$final_string" echo $final_string > "$file_final" fi } ## Main bar loop ## function set-bar { # variables holding values in seconds declare -i status_loop=1 declare -i date_loop=60 declare -i battery_loop=178 declare -i volume_loop=35997 declare -i network_loop=4 declare -i ssh_loop=9 declare -i brightness_loop=35996 declare -i weather_loop=175 # start relevant update-* function every N seconds while true; do if (( date_loop==60 )); then update-date date_loop=1 else ((date_loop++)) fi if (( battery_loop==180 )); then update-battery battery_loop=1 else ((battery_loop++)) fi if (( volume_loop==36000 )); then update-volume volume_loop=1 else ((volume_loop++)) fi if (( network_loop==5 )); then update-network network_loop=1 else ((network_loop++)) fi if (( ssh_loop==10 )); then update-ssh ssh_loop=1 else ((ssh_loop++)) fi if (( brightness_loop==36000 )); then update-brightness brightness_loop=1 else ((brightness_loop++)) fi if (( weather_loop==1800 )); then update-weather weather_loop=1 else ((weather_loop++)) fi if (( status_loop==10 )); then update-status status_loop=1 else ((status_loop++)) fi # make the thread sleep for 1 second sleep 1s done } ################### ## SET FUNCTIONS ## ################### ## Control the volume ## function volume { # media key initiating the function with one of the below args based on config.h if [ "$1" == "inc" ] then amixer -c 1 set Master 2%+ &>/dev/null elif [ "$1" == "dec" ] then amixer -c 1 set Master 2%- &>/dev/null elif [ "$1" == "mute" ] then amixer -c 1 set Master 0+ toggle &>/dev/null fi # update the new string in the update-volume function update-volume } ## Control the brightness ## function brightness { # brightness keys initiating the function with one of the below args based on config.h if [ "$1" == "inc" ] then new_brightness=$(expr $(cat $actual_brightness) + 5) elif [ "$1" == "dec" ] then new_brightness=$(expr $(cat $actual_brightness) - 5) fi echo "$new_brightness" > $actual_brightness # update the new string in the update-brightness function update-brightness } ###################### ## UPDATE FUNCTIONS ## ###################### ## Update the volume ## function update-volume { # string to check the current volume % actual_volume="$(amixer -c1 | head -5 | tail -1 | awk -F '[][]' '{print $2}')" # check if currently we are muted ot not if [ "$(amixer -c1 | head -5 | tail -1 | awk -F '[][]' '{print $6}')" == "off" ] then current_volume="" else current_volume=" $actual_volume" fi # create the temp file holding our value echo $current_volume > $file_volume # update string in the update-all function update-all } function update-battery { # get current battery state and it's capacity state_battery=$(cat /sys/class/power_supply/BAT0/status) value_battery=$(echo -e "$(cat /sys/class/power_supply/BAT0/capacity)%") # check if the battery is charging or not and give a different icon based on it if [ "$state_battery" == "Discharging" ] then string_battery=" $value_battery" else string_battery=" $value_battery" fi # create the temp file holding our value echo $string_battery > $file_battery # update string in the update-all function update-all } function update-brightness { # string holding the current brightness of the screen string_brightness=$(echo -e " $(cat /sys/class/backlight/amdgpu_bl0/brightness)%") # create the temp file holding our value echo -e "$string_brightness" > $file_brightness # update string in the update-all function update-all } function update-network { # THIS FUNCTION NEEDS MORE FUNCTIONALITY LOL # Check if we are connected to the wifi and show the current SSID if so, otherwise just say disconnected.. if iwctl station wlan0 show | grep "State connected" > /dev/null then state_network=" $(iwctl station wlan0 show | cut -c 33- | head -7 | tail -1)  $(ping -c5 1.1.1.1 | tail -1 | awk -F '/' '{print $5}' | awk -F '.' '{print $1}') ms" else state_network=" disconnected" fi # create the temp file holding our value echo -e $state_network > $file_network # update string in the update-all function update-all } function update-ssh { # count the number of ssh connections state_ssh=$(pgrep ssh | wc -l) if [ $state_ssh -eq 0 ] then value_ssh="N/A" else value_ssh="$state_ssh" fi # create the temp file holding our value echo -e "SSH:($value_ssh)" > $file_ssh # update string in the update-all function update-all } function update-status { # calculate the CPU percentage used from /proc/stat cpu_line=$(awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)) # split the above in 2 strings to force only 2 digits after decimal point and don't show duplicate % signs cpu_st1=$(echo $cpu_line | awk -F '.' '{print $1}') cpu_st2=$(echo $cpu_line | awk -F '.' '{print $2}' | cut -c -2 | grep -v "%") # string to get the load average; currently not used as no space left on the status bar.. =[ loadav_status=$(cat /proc/loadavg | awk -F '/' '{print $1}') # string to get the free memory of the server memory_status=$(free -gh | grep Mem: | awk '{print $4"/"$2}') # create the temp file holding our value echo -e " $cpu_st1.$cpu_st2%  $memory_status" > $file_status # update string in the update-all function update-all } function update-date { # time and date format #current=$(date '+%A %e-%b-%y @ %H:%M') current_date=$(date '+%e-%b-%y @%H:%M') # create the temp file holding our value echo -e " $current_date" > $file_date # update string in the update-all function update-all } function update-weather { # weather string format #current=$(curl -s wttr.in/Burnham,Buckinghamshire\?format\="%C,+%t+%w+%q\n") current_weather=$(curl -s wttr.in/Burnham,Buckinghamshire\?format\="%t+%w+%q\n") # create the temp file holding our value echo -e " $current_weather" > $file_weather # update string in the update-all function update-all } ############################# ## INITIATE THE START-LOOP ## ############################# start-loop "$@"