brightness (858B) - Raw
1 #!/usr/bin/env bash 2 # 3 # This is a shell "application", but doesn't have to be one, so 4 # leaving the shebang in place. 5 set -euo pipefail 6 7 dir=$(find /sys/devices/ -name intel_backlight | head -1) 8 if [[ -z "$dir" ]]; then 9 echo "intel_backlight not found" 10 exit 1 11 fi 12 13 max=$(cat "$dir/max_brightness") 14 now=$(cat "$dir/brightness") 15 step=$((max * 5 / 100)) 16 min=100 17 18 clamp() { 19 if [[ $1 -gt $max ]]; then 20 echo "$max" 21 elif [[ $1 -lt $min ]]; then 22 echo "$min" 23 else 24 echo "$1" 25 fi 26 } 27 28 info() { 29 perc="$(echo "$1*100/$max" | bc -l)" 30 printf "%s of %s (%.2f%%)\n" "$1" "$max" "$perc" 31 } 32 33 new= 34 case "${1:-}" in 35 up) 36 new=$(clamp $((now + step)) ) ;; 37 down) 38 new=$(clamp $((now - step)) ) ;; 39 esac 40 41 if [[ -n "$new" ]]; then 42 echo "$new" | sudo tee "$dir/brightness" > /dev/null 43 info "$new" 44 else 45 info "$now" 46 fi