0

I made a little program to manage the brightness of the screen for my laptop. the problem is that when i overwrite the BRIGHTNESSCTRL variable, well, i overwrite it and the previous value is erased, so when i use the program always it sets the brightness to 20000, any help is appreciated. By the way this is my program.

#!/bin/bash
echo 'Actual brightness:' ; cat /sys/class/backlight/intel_backlight/actual_brightness 

cat /sys/class/backlight/intel_backlight/actual_brightness > BRIGHTNESSCTRL

let "BRIGHTNESSCTRL=BRIGHTNESSCTRL+10000"

if [[ $BRIGHTNESSCTRL -gt 96000 ]] ; then BRIGHTNESSCTRL=96000 ; fi && echo 'Success' || echo 'Failure' 

echo $BRIGHTNESSCTRL

sudo echo $BRIGHTNESSCTRL > /sys/class/backlight/intel_backlight/brightness ; echo $BRIGHTNESSCTRL
  • See [How to read a file into a variable in shell?](https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell) – that other guy May 01 '21 at 17:38
  • Where do you set variable BRIGHTNESSCTRL with a value? `> BRIGHTNESSCTRL` redirects output to a file named BRIGHTNESSCTRL. – Cyrus May 01 '21 at 17:41
  • how/when do you determine that the 'previous' value should be used? at the beginning of the script? upon initiating a new shell/session? what happens if you run the script multiple times during the same shell/session ... which brightness setting would you expect to be used? assuming you know what the 'previous' value is(was), I'd probably opt for a variable to hold a 'default' value (perhaps add to your `.profile/.bashrc`), then modify this script to display the 'default' value as well as the 'current' value, perhaps give the user an option to pick one of the two or manually enter a 3rd value – markp-fuso May 01 '21 at 18:25

1 Answers1

1

You seem to be confusing a file with a shell variable. The first command writes the original value to a file, but you are never reading that file.

Your question is rather unclear, but I guess you are looking for something like

#!/bin/bash
brightness=$(cat /sys/class/backlight/intel_backlight/actual_brightness)

echo "Actual brightness: $brightness"

brightness=$((brightness+10000))

if [[ $brightness -gt 96000 ]] ; then
    brightness=96000
fi

echo $brightness | sudo tee /sys/class/backlight/intel_backlight/brightness

It's unclear when you hoped echo "Success" or echo "Failure" to happen, so I took those out. Notice that sudo only provides privileged access to the individual command you run (in your case, that was echo), not to any redirections performed by the shell. You should prefer lower case for private variables.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • It seems like i was confused about the difference about assign the output to a var or to a file, i am a newbie in this, also the problem about redirecting i readed about it but i didn't realized the reason of the error, for avoid that error i changed the file permissions but i'll reset them and use 'tee' instead for overwrite instead of redirect the output indirectly, thank you pal :) – Lucastegano X May 01 '21 at 21:39