xrandr --output LVDS-1 --brightness $(bc -l <<<"$(sed 's/Brightness: //' <<<$(xrandr --prop --verbose | grep Brightness)) $(if [ "$1" = "+" ];then echo +;else echo -;fi) 0.1")
I currently have this code set up to a keyboard shortcut in XFCE on my Arch system, and it works great to adjust brightness up or down 0.1 notches. Only problem is, it's slow enough to take a second or so to execute, and pressing multiple times is worse and can slow the whole computer a bit.
How would you improve the code? (I'm pretty new to shell scripting, so I'm also partly asking this to see the process by which code I write cold be optimized.)
EDIT: Okay, so I followed drewbenn's suggestion of profiling and got this:
time xrandr -q --verbose > /dev/null
real 0m1.746s
user 0m0.007s
sys 0m0.000s
The query, then, was the biggest issue. I changed it so that it would store the current brightness level in a file somewhere and the shortcut keys would run the following code:
#!/bin/bash
val=$(cat ~/.bright_key_folder/lvl)
if ( [ "$1" == "+" ] && [[ $(bc -l ~/.bright_key_folder/lvl
xrandr --output LVDS-1 --brightness $(cat ~/.bright_key_folder/lvl)
elif ( [ "$1" == "-" ] && [[ $(bc -l 0") == 1 ]] )
then
bc -l ~/.bright_key_folder/lvl
xrandr --output LVDS-1 --brightness $(cat ~/.bright_key_folder/lvl)
fi
Then on startup it resets brightness and the file value both to 1.
xbacklight. It may or may not work for (I think they go through different mechanisms and not all drivers support both). It may or may not be faster.