Created
May 10, 2024 10:34
-
-
Save larsch/2679c99c522603e58a1647ae8258a62b to your computer and use it in GitHub Desktop.
Shell script to adjust backlight by absolute or relative values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
backlight=$(find /sys/class/backlight -type l -print -quit) | |
if [ -z "$backlight" ]; then | |
echo "Backlight not found" | |
exit 1 | |
fi | |
max_brightness=$(cat "$backlight/max_brightness") | |
current_brightness=$(cat "$backlight/brightness") | |
case "$1" in | |
+[0-9]*%) | |
percentage="${1#"+"}" | |
percentage="${percentage%"%"}" | |
change=$(((current_brightness * percentage + 50) / 100)) | |
if [ $percentage -ne 0 ] && [ $change -eq 0 ]; then | |
change=1 | |
fi | |
new_brightness=$((current_brightness + change)) | |
;; | |
-[0-9]*%) | |
percentage="${1#"-"}" | |
percentage="${percentage%"%"}" | |
change=$(((current_brightness * percentage + 50) / 100)) | |
if [ $percentage -ne 0 ] && [ $change -eq 0 ]; then | |
change=1 | |
fi | |
new_brightness=$((current_brightness - change)) | |
;; | |
+[0-9]*) | |
change="${1#"+"}" | |
new_brightness=$((current_brightness + change)) | |
;; | |
-[0-9]*) | |
change="${1#"-"}" | |
new_brightness=$((current_brightness - change)) | |
;; | |
[0-9]*) | |
new_brightness="$1" | |
;; | |
"") | |
echo Usage: | |
echo " light.sh <ABSVAL> - set absolute value (0-255)" | |
echo " light.sh [+-]<ABSDELTA> - add/subtract from absolute value" | |
echo " light.sh [+-]<REL>% - change relative to current level" | |
exit 0 | |
;; | |
*) | |
echo "Invalid backlight change (either absolute value, +/-delta, or +/-relative%)" | |
exit 1 | |
;; | |
esac | |
if [ $new_brightness -gt $max_brightness ]; then | |
new_brightness=$max_brightness | |
elif [ $new_brightness -lt 0 ]; then | |
new_brightness=0 | |
fi | |
echo $new_brightness > "$backlight/brightness" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment