[SOLVED] Help with simple script to toggle gamma values on lcd monitor
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
[SOLVED] Help with simple script to toggle gamma values on lcd monitor
Wonderful people:
I am trying to make an extremely simple script to toggle the gamma settings in my lcd monitor between two values.
This is what I did with no success so far:
Code:
#!/bin/bash -x
# 0.90 1.20
PresentValue=`xgamma`
if [ "$PresentValue" = "-> Red 0.900, Green 0.900, Blue 0.900 " ] ; then
echo "0.900"
xgamma -rgamma 1.20 -ggamma 1.20 -bgamma 1.20 2> /dev/null
else
xgamma -rgamma 0.90 -ggamma 0.90 -bgamma 0.90 2> /dev/null
fi
The problem, I think is the following:
when I run xgamma, I get the following line:
Code:
-> Red 0.900, Green 0.900, Blue 0.900
but this is not being taken as the actual output of the command in the script.
The double quotes on PresentValue="$(xgamma)" are essential in case the returned value contains whitespace (which it does). Changing from ` ` to $( ) is good practice.
The single quotes around $PresentValue in the DEBUG print are there to make any leading or trailing whitespace characters visible.
I did.
I did what Catkin suggested and also what you suggested.
I am still stuck.
As I said, the problem seems to be the following:
Code:
PresentValue=`ls`; echo "$PresentValue"
obviously returns the output of ls.
Code:
PresentValue=`xgamma`; echo "$PresentValue"
returns nothing.
(PS: I thank you for your help and willingness to help people who are not as learned as you are).
Ha! I just figured it out. It happens that I have "xgamma" on this system, and its output is going to "stderr", not "stdout". Unices have two pre-opened streams for CLI applications:
stdout: normal text output.
stderr: error messages.
For some reason, xgamma is printing to stderr instead of stdout. But you can re-route stderr to stdout. Like this:
Ha! I just figured it out. It happens that I have "xgamma" on this system, and its output is going to "stderr", not "stdout". Unices have two pre-opened streams for CLI applications:
stdout: normal text output.
stderr: error messages.
For some reason, xgamma is printing to stderr instead of stdout. But you can re-route stderr to stdout. Like this:
if [ "$PresentValue" = "-> Red 0.900, Green 0.900, Blue 0.900 " ] ; then
should be:
Code:
if [ "$PresentValue" == "-> Red 0.900, Green 0.900, Blue 0.900 " ] ; then
In this situation = and == work identically, as described in the GNU Bash Reference, where it says "‘=’ may be used in place of ‘==’ for strict posix compliance". Some prefer the == because it is obviously not an assignment operator. Others prefer = for historical reasons (and takes less typing!)
The quotes around the string constant can be double or single. If you use single quotes then it is easier to read -- the string is used exactly as it stands until the closing single quote whereas a double quoted string has to be scanned for characters that are special to the shell such as $, \, ` etc.
Quote:
Originally Posted by lutusp
Your test print:
Code:
echo "DEBUG: PresentValue: '$PresentValue'"
Should be:
Code:
echo "DEBUG: PresentValue: \"$PresentValue\""
That is correct if you want the value of $PresentValue to be displayed within double quotes. There is nothing wrong with
Code:
echo "DEBUG: PresentValue: '$PresentValue'"
if you want the value of $PresentValue to be displayed within single quotes. I prefer the latter -- it is "cleaner", both in the script and in the output.
Thank you all guys. It worked wonderfully.
Thanks also for the indications on the equal sign.
(I'm sorry about the lack of professionality of the backticks. As is obvious, I'm not a professional; this is not what I do in real life; it's just a wfie-and-kids-gone-to-bed-hobby).
Yes, and this is extremely bad advice to be giving newbies, many of whom are studying computer programming, and many of whom will take it to mean there is no difference bewtween '=' and '==' in general. The fact that Bash accepts both only shows a lack of consistency and intellectual discipline.
Quote:
Originally Posted by catkin
The quotes around the string constant can be double or single.
Same reply! This is very bad advice for newbies, who may not know the general difference between the two, and who may therefore not understand that:
$ echo "$PresentValue"
and
$ echo '$PresentValue'
produce different results, and why. For these students, explaining that --
$ echo "$PresentValue"
-- and --
$ echo "'$PresentValue'"
-- both work after a fashion is a conversation not worth having, and one that can only sow confusion.
This syntactical ambiguity will confuse relative newcomers to the world of computer programming, and has no other purpose.
Quote:
Originally Posted by catkin
the string is used exactly as it stands until the closing single quote whereas a double quoted string has to be scanned for characters that are special to the shell such as $, \, ` etc.
Utter nonsense. If it were true, the single-quoted version would not have printed the string's contents. In fact, in this example the single-quoted and double-quoted versions are both scanned for special sequences and the only difference between the two is the fact that one prints single quotes and the other prints double quotes -- that's all.
Quote:
Originally Posted by catkin
I prefer the latter -- it is "cleaner", both in the script and in the output.
Thanks for sharing your personal tastes. This is about making computer programming and shell scripting comprehensible to newbies. Consistent syntax furthers that goal. Not making side trips to Alice's wonderland furthers that goal.
lutusp:
If your concern is giving good advice to newbies like me, I would suggest considering manners: I've been a teacher for a long time, and one thing I've learnt is that politeness is the requisite for learning (on both parts).
Asking the person who asked a question if he has bothered to read something or that some of his habits (such as using backticks) are not, I think, conducive to effectively sharing your knowledge.
That said, I thank you again for the information and advice.
the string is used exactly as it stands until the closing single quote whereas a double quoted string has to be scanned for characters that are special to the shell such as $, \, ` etc.
Utter nonsense. If it were true, the single-quoted version would not have printed the string's contents. In fact, in this example the single-quoted and double-quoted versions are both scanned for special sequences and the only difference between the two is the fact that one prints single quotes and the other prints double quotes -- that's all.
The prior poster's claim was that within this string --
Code:
echo "DEBUG: PresentValue: '$PresentValue'"
-- the single-quoted section would not be scanned for special sequences. The prior poster was mistaken. I said so.
Ah! Misunderstanding! I presume you are referring to
Quote:
The quotes around the string constant can be double or single. If you use single quotes then it is easier to read -- the string is used exactly as it stands until the closing single quote whereas a double quoted string has to be scanned for characters that are special to the shell such as $, \, ` etc.
When I wrote that I was referring to the "-> Red 0.900, Green 0.900, Blue 0.900 " above and not the "DEBUG: PresentValue: '$PresentValue'" below. Sorry for the ambiguity.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.