Terminal screen brightness bash script with desktop file

Moderator: Forum moderators

Post Reply
geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Terminal screen brightness bash script with desktop file

Post by geo_c »

EDIT: updated the script 2023-07-16 to include contrast (xgamma) and temperature (sct)

Note: This script requires sct to be installed to run correctly, which most systems will likely have.

A script to change the display monitor temperature/brightness/contrast by entering numbers in the terminal.

You should be able to put this script in /root/my-applications/bin in puppies, make it executable, and run it in a terminal just by typing brightness (or whatever name you choose for the script):

brightness.tar.gz
(648 Bytes) Downloaded 25 times

Code: Select all

#!/bin/bash

echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                    COLOR TEMPERATURE                   |"
echo " |            Enter value between 1000 and 10,000         |"
echo " |            note: 6500 is the median temperature        |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter TEMP: ' TEMP
sct $TEMP
echo ""
echo ""
MONITOR="$(xrandr | grep " connected" | cut -f1 -d " ")"
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                  BRIGHTNESS/BACKLIGHT                  |"
echo " |            Enter value between 0.5 and 1.3             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |    note: Some monitors may handle values above 1.3     |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter BACKLIGHT: ' LEVEL
xrandr --output $MONITOR --brightness $LEVEL
echo ""
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                        CONTRAST                        |"
echo " |            Enter value between 0.3 and 1.5             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter CONTRAST: ' CONTRAST
xgamma -gamma $CONTRAST
echo ""
echo ""
printf "temperature = "$TEMP
echo ""
printf "brightness  = "$LEVEL
echo""
printf "contrast    = "$CONTRAST
echo ""
echo ""
GOODBYE=$(
read -p 'Enter settings again? y for yes, any character to exit: ' LEAVE
if [ $LEAVE = 'y' ]; then
	/root/my-applications/bin/brightness
fi )
$GOODBYE

And the desktop entry

brightness.desktop.tar.gz
(285 Bytes) Downloaded 32 times

Code: Select all

[Desktop Entry]
Type=Application
Version=1.0
Name=brightness
GenericName=Display Brightness
Comment=Adjust Screen Brightness
Icon=utility48
Exec=brightness
Terminal=true
Categories=System;Utility;ConsoleOnly;
Keywords=system;brightness
Last edited by geo_c on Sun Jul 23, 2023 3:51 am, edited 15 times in total.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

Added a desktop file to the first post.

geo_c
Old School Hipster, and Such

User avatar
mikewalsh
Moderator
Posts: 6250
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 825 times
Been thanked: 2016 times

Re: Terminal screen brightness shell script with desktop file

Post by mikewalsh »

@geo_c :-

You've forgotten the all-important first line of the .desktop entry, mate:-

Code: Select all

[Desktop Entry]

AFAIK, without this defining line at the beginning, the system won't know what to do with it. I'd be surprised if it will show in the Menu. See:-

viewtopic.php?t=3420

...and:-

https://wiki.archlinux.org/title/Desktop_entries

Mike. ;)

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

mikewalsh wrote: Sat Jun 17, 2023 7:36 pm

You've forgotten the all-important first line of the .desktop entry, mate:-

Code: Select all

[Desktop Entry]

Oh I see what happened. My desktop file has the top entry, but it's missing in the code I pasted in the original post. The attached brightness.desktop.tar.gz has the necessary top line.

Will fix the posted code.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

Hey @mikewalsh, (or anyone else!)

I've been playing around with commands in terminal, so I did a silly thing and I defined $here as the pwd, as in this command that I ran:

Code: Select all

here="$(pwd)"; mkdir -v $here/TEST

Which created the /TEST directory in the present working directory. Cool, it worked, but then I thought about how I would never need that command, because whatever is pwd doesn't need to be referenced. At least I can't think of a reason at the moment.

I thought $here would be known as a "function." So I was searching for how to unset a function. And it doesn't appear to be a function. Is it a variable?

If I want to get rid of $here in bash, can I use the unset command? What command would I use, and where are these definitions stored?

maybe like:

Code: Select all

unset here

geo_c
Old School Hipster, and Such

User avatar
mikewalsh
Moderator
Posts: 6250
Joined: Tue Dec 03, 2019 1:40 pm
Location: King's Lynn, UK
Has thanked: 825 times
Been thanked: 2016 times

Re: Terminal screen brightness shell script with desktop file

Post by mikewalsh »

@geo_c :-

Mm. As far as I'm aware, anything referenced with the dollar sign ("$") is usually a variable. I think functions are defined using the pair of parentheses (brackets).....like so:-

Code: Select all

function_name ()

I'm probably wrong. My scripting skills are still pretty basic, so.....don't take that as gospel!

I'm the last person to ask this of. I'm still in the early stages of learning myself, though it is possible to concoct pretty complex scripts using rather basic concepts....

(*shrug*)

EDIT:- These might help:-

https://linuxize.com/post/bash-functions/

https://phoenixnap.com/kb/bash-function

Mike. ;)

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

mikewalsh wrote: Sat Jun 17, 2023 11:42 pm

I'm the last person to ask this of. I'm still in the early stages of learning myself, though it is possible to concoct pretty complex scripts using rather basic concepts....
Mike. ;)

Well I figured out where the functions are stored, I believe in bash.rc, that's where I recently added a bookmark script for the terminal.

But I haven't figured out where the variables are stored, apparently some kind of table somewhere.

However I figured out how to delete the variable, I rolled back to a very current backup copy of upper_changes! Solved that problem.

I love these OS's for that reason among many.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

Back to the script itself, I wrote a prompt that says enter a value between .5 and 1.0, but reading my /sys/class/backlight/nv_backlight/max_brightness file it appears to me that the value can go as high as 3.0, but I'm guessing that might be damaging to the monitor, and look very bad.

I think a value as high as 1.5, maybe 2.0 is probably about as far as I would want to go. And I don't know if all monitors have the same max value.

This little scipt is no way as good as display control built into many puppies, which allows for foreground brightness and backlight. There may be a way to do that in the terminal also.

geo_c
Old School Hipster, and Such

williwaw
Posts: 1975
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 172 times
Been thanked: 372 times

Re: Terminal screen brightness shell script with desktop file

Post by williwaw »

geo_c wrote: Sat Jun 17, 2023 10:59 pm

here="$(pwd)"; mkdir -v $here/TEST

a rollback is too extreme, try just closing the terminal to exit the shell (presuming you are working in a terminal emulator like xterm? on the desktop)

echo $here should return the pwd, but if you close xterm and open a new instance, the command echo $here returns an empty line

you can make things permanent by adding to .bashrc

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

williwaw wrote: Sun Jun 18, 2023 3:37 am
geo_c wrote: Sat Jun 17, 2023 10:59 pm

here="$(pwd)"; mkdir -v $here/TEST

a rollback is too extreme, try just closing the terminal to exit the shell (presuming you are working in a terminal emulator like xterm? on the desktop)

echo $here should return the pwd, but if you close xterm and open a new instance, the command echo $here returns an empty line

you can make things permanent by adding to .bashrc

Well the thing is, @here was fully functioning even after rebooting the entire OS (KLV-Spectr) so I was a bit surprised. I also thought closing the terminal would do it.

geo_c
Old School Hipster, and Such

User avatar
MochiMoppel
Posts: 1258
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 456 times

Re: Terminal screen brightness shell script with desktop file

Post by MochiMoppel »

geo_c wrote: Sun Jun 18, 2023 4:02 am

Well the thing is, @here was fully functioning even after rebooting the entire OS (KLV-Spectr) so I was a bit surprised. I also thought closing the terminal would do it.

You don't need a $here variable. Better use $PWD. This variable is maintained by the shell and always contains your current working directory.
This means
mkdir -v $PWD/TEST
is the same as
here="$(pwd)"; mkdir -v $here/TEST

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

MochiMoppel wrote: Sun Jun 18, 2023 5:08 am

You don't need a $here variable. Better use $PWD. This variable is maintained by the shell and always contains your current working directory.
This means
mkdir -v $PWD/TEST
is the same as
here="$(pwd)"; mkdir -v $here/TEST

Yes, this I realized, and I was really only playing around with syntax and experimenting. I just used the @here variable because it was the first thing I thought of. And that's why I'm trying to figure out how to unset a variable in the shell.

I thought it would be gone once the shell closed, or I rebooted, but it was still there. I wasn't using a script, I was just typing commands.

Last edited by geo_c on Sun Jun 18, 2023 5:01 pm, edited 1 time in total.

geo_c
Old School Hipster, and Such

User avatar
fredx181
Posts: 3159
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 386 times
Been thanked: 1356 times
Contact:

Re: Terminal screen brightness shell script with desktop file

Post by fredx181 »

geo_c wrote:

I thought it would be gone once the shell closed, or I rebooted, but it was still there. I wasn;t using a script, I was just typing commands.

That should not be, did you add the here= variable in ~/.bashrc perhaps ? (edit: or in .bash_profile)
edit: typing env in a terminal will show all the variables loaded at login, if the here variable is part of it, something is wrong (if unintended).

williwaw
Posts: 1975
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 172 times
Been thanked: 372 times

Re: Terminal screen brightness shell script with desktop file

Post by williwaw »

that's where I recently added a bookmark script for the terminal.

can you share your .bashrc?

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

fredx181 wrote: Sun Jun 18, 2023 10:47 am

That should not be, did you add the here= variable in ~/.bashrc perhaps ? (edit: or in .bash_profile)

Well it was present because I checked it three ways, I restarted x once, and rebooted twice, each time entering: dir $here which gave me a list of files in the pwd.

Since I reverted to a backup upper_changes, I can't check again, and I probably should have played around with it more before ditching.

I definitely didn't add it to bashrc, because I checked all the bash files I could find.

KLV-spectr has xterm and uxterm, do they read from the same variables?

I'm super curious so I'm thinking of trying it out again, maybe with a different variable.

Is it at all possible that defining a variable as another variable (pwd) is bug prone?

EDIT: See post below

Last edited by geo_c on Sun Jun 18, 2023 5:36 pm, edited 1 time in total.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

williwaw wrote: Sun Jun 18, 2023 5:08 pm

that's where I recently added a bookmark script for the terminal.

can you share your .bashrc?

Uh, yeah, I think the problem may be right at the top of the bashrc where it says, "Do not edit this file..."

I'm pretty sure I added the bookmark function right to the bashrc

Code: Select all

# /etc/bash/bashrc

# Do not edit this file.
# Place your readable configs in /etc/bash/bashrc.d/*.sh 

if [[ $- != *i* ]] ; then
	# Shell is non-interactive.  Be done now!
	return
fi

if [ -d /etc/bash/bashrc.d/ ]; then
	for f in /etc/bash/bashrc.d/*.sh; do
		[ -r "$f" ] && . "$f"
	done
	unset f
fi

# cd bookmarks
# function to add a bookmark; usage: bm [<bookmark_name>], run again to remove
bm () { local b="/root/puppy-reference/${1:-$(basename $(pwd))}"; ln -svn "$(pwd)" "$b" || rm -i "$b"; }
# set CDPATH, don't export it (it can be without a leading ":")
CDPATH=":/root/puppy-reference"
# cd alias that dereferences links
alias cdd='cd -P'
# set bash-completion for cdd and bm
complete -o nospace -F _cd cdd
complete -o nospace -F _cd bm

So is it correct that what the top of the rc file says is to add a file etc/bash/bashrc.d/bookmarks.sh and place my bookmark function in that file?

Last edited by geo_c on Sun Jun 18, 2023 5:39 pm, edited 4 times in total.

geo_c
Old School Hipster, and Such

User avatar
fredx181
Posts: 3159
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 386 times
Been thanked: 1356 times
Contact:

Re: Terminal screen brightness shell script with desktop file

Post by fredx181 »

geo_c wrote:

each time entering: dir $here which gave me a list of files in the pwd

Ah, getting somewhere now, dir $here is not a good test because if $here is empty (I guess it is) it would be the same as just dir and will show the files in current directory.
(similar for e.g. dir $blablablah)
To really test if $here variable is empty (or not) would be e.g. echo $here.

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

fredx181 wrote: Sun Jun 18, 2023 5:35 pm
geo_c wrote:

each time entering: dir $here which gave me a list of files in the pwd

Ah, getting somewhere now, dir $here is not a good test because if $here is empty (I guess it is) it would be the same as just dir and will show the files in current directory.
(similar for e.g. dir $blablablah)
To really test if $here variable is empty (or not) would be e.g. echo $here.

Ah! [faceplant!] :mrgreen:

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

So I just learned how to use the xgamma -gamma command to adjust the contrast, so I wrote a script for that setting also, and combined them to adjust both values.

Code: Select all

MONITOR="$(xrandr | grep " connected" | cut -f1 -d " ")"
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                  BRIGHTNESS/BACKLIGHT                  |"
echo " |            Enter value between 0.5 and 1.0             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter value: ' LEVEL
xrandr --output $MONITOR --brightness $LEVEL
echo ""
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                        CONTRAST                        |"
echo " |            Enter value between 0.3 and 1.5             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter value: ' CONTRAST
xgamma -gamma $CONTRAST

This new script has been added to the first post along with a new tar.gz

geo_c
Old School Hipster, and Such

williwaw
Posts: 1975
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 172 times
Been thanked: 372 times

Re: Terminal screen brightness shell script with desktop file

Post by williwaw »

Geo
have you tried a bash alias for simple stuff?
on easy, brightness can be set by calling /usr/local/dcontrol/brightness-set
so I can # alias b=" /usr/local/dcontrol/brightness-set" and then just type
# b 75 to lower the brightness to 75%

# alias b=" /usr/local/dcontrol/brightness-set" can aslo be added to .bashrc to be persistent

you may already have the script # sct on your pup for temprature control
I prefer # sct 4500 after the sun goes down

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

williwaw wrote: Tue Jun 20, 2023 9:08 pm

Geo
have you tried a bash alias for simple stuff?

No I haven't used alias's at all. So I'll be giving your suggestions a whirl. Your alias b idea is great, but it doesn't have the primitive graphics that my script does ;)

edit: and the reason I'm writing this script is because I'm using KLV-Spectr which comes pretty bare bones, so no brightness utilities are present on the system like dcontrol or sct.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

williwaw wrote: Tue Jun 20, 2023 9:08 pm

you may already have the script # sct on your pup for temprature control
I prefer # sct 4500 after the sun goes down

Okay so I just installed sct on KLV-Spectrwm, and now I'll include that in my script.

This script is also my attempt to learn how to construct an interactive script using the read command.

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

Here's a new script, with temperature control added. It requires the sct utility to be installed on the system. It also summarizes the levels and requires a key to exit.

Next I'd like to add a loop to allow the settings to be set again and press a Y to exit or N to loop back into the settings. But that's for another day.

Code: Select all

#!/bin/bash

MONITOR="$(xrandr | grep " connected" | cut -f1 -d " ")"
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                  BRIGHTNESS/BACKLIGHT                  |"
echo " |            Enter value between 0.5 and 1.0             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter value: ' LEVEL
xrandr --output $MONITOR --brightness $LEVEL
echo ""
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                        CONTRAST                        |"
echo " |            Enter value between 0.3 and 1.5             |"
echo " |    WARNING! Lower values are dangerously invisible!    |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter value: ' CONTRAST
xgamma -gamma $CONTRAST
echo ""
echo ""
echo "  ________________________________________________________"
echo " |                                                        |"
echo " |                    COLOR TEMPERATURE                   |"
echo " |            Enter value between 1000 and 10,000         |"
echo " |________________________________________________________|"
echo ""
read -p '                    Enter value: ' TEMP
sct $TEMP
echo ""
echo ""
printf "brightness  = "$LEVEL
echo""
printf "contrast    = "$CONTRAST
echo ""
printf "temperature = "$TEMP
echo ""
echo ""
read -p 'Press any key to exit ' LEAVE
echo exiting...
sleep 1

geo_c
Old School Hipster, and Such

geo_c
Posts: 2886
Joined: Fri Jul 31, 2020 3:37 am
Has thanked: 2216 times
Been thanked: 884 times

Re: Terminal screen brightness shell script with desktop file

Post by geo_c »

The script has been updated and uploaded to the first post in this topic: viewtopic.php?p=91779#p91779

Now includes color temperature control, backlight, and contrast.

On my system the sct color temp control seems to reset the backlight, so it was necessary to put that setting 1st in the script.

The script prompts in this order:

Color temperature (value between 1000 and 10,000)

Backlight (value between 0 and 1.3, though lower than 7 can be dangerously dark, and values higher than 1.3 may depend on the monitor as to how safe they are to use. Mine will handle 2.0 and higher, but I don't go higher than 1.3)

Contrast (value between .5 and 1.2 seems to work, though lower brings overall visibility down, and higher washes out the display.)

The settings can be entered again by typing "y" and pressing "enter" at the last prompt. Any other key+enter or simply "enter" with no entry will exit.

geo_c
Old School Hipster, and Such

Post Reply

Return to “Utilities”