I have been using a bash script within ChromeOS to record screen brightness every 5 minutes as well as change and record the brightness if it changes.
#!/bin/sh
#This script will collect brightness every 5 minutes
#If brightness is not at desired setting, script will correct and note time changed
i=13
time=$(date %H%M)
bright=$(backlight_tool --get_brightness_percent)
echo "start time is "$time >> brightness_log.txt
while [ $i -ne 1 ]; do
sleep 300
backlight_tool --get_brightness_percent >> brightness_log.txt
i=$(( $i-1 ))
if [ $bright != 60.5 ]; then
backlight_tool --set_brightness_percent=60.5
echo "Brightness changed at "$time >> brightness_log.txt
else
break
fi
done
exit
The script works, however even when brightness is 60.5, the if statement will execute and echo "Brightness changed at " with the same repeated time.
If the brightness IS equal to 60.5, why is the if statement executing and not skipping to the break?
CodePudding user response:
You never update the variable bright it's set outside of the loop. Move the assignment inside the while loop, and it will work.
while [ "$i" -gt 1 ]; do
bright=$(backlight_tool --get_brightness_percent)
..
done
Please validate your script with shellcheck, as you have many pitfalls present, one being not quoting parameter expansion:
$ shellcheck myscript
Line 10:
echo "start time is "$time >> brightness_log.txt
^-- SC2086 (info): Double quote to prevent globbing and word splitting.
CodePudding user response:
Within the while loop I inserted
echo "$bright" | grep "60.5" -q
gcode=$?
I then changed the if statement to
if [[ $gcode -eq 0 ]]; then
The if statement now only executes as intended.
