Lucastegano X
Lucastegano X

Reputation: 5

How to keep the previous value of a variable in bash

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

Upvotes: 0

Views: 629

Answers (1)

tripleee
tripleee

Reputation: 189387

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.

Upvotes: 1

Related Questions