LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 09-23-2009, 04:12 PM   #1
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Rep: Reputation: 15
[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.

Any help, ideas, etc.?

Last edited by musonio; 09-24-2009 at 05:20 AM.
 
Old 09-23-2009, 04:21 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Code:
PresentValue="$(xgamma)"
echo "DEBUG: PresentValue: '$PresentValue'"
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.
 
Old 09-23-2009, 04:34 PM   #3
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
Catkin:
Thanks for your answer, but I am still stuck.
If I do:

Code:
echo "DEBUG: PresentValue: '$PresentValue'"
I get:

Code:
DEBUG: PresentValue: ''
If I do

Code:
echo "$PresentValue"
I also get nothing.

Am I doing something (else) wrong?
 
Old 09-23-2009, 05:56 PM   #4
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by musonio View Post
Catkin:
Thanks for your answer, but I am still stuck.
If I do:

Code:
echo "DEBUG: PresentValue: '$PresentValue'"
I get:

Code:
DEBUG: PresentValue: ''
If I do

Code:
echo "$PresentValue"
I also get nothing.

Am I doing something (else) wrong?
Did you read the post you are replying to? Do not use backticks, use parentheses.

NO: PresentValue=`xgamma`

YES: PresentValue="$(xgamma)"

Then test to see if you have captured what you think you have.

Code:
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
Your test print:

Code:
echo "DEBUG: PresentValue: '$PresentValue'"
Should be:

Code:
echo "DEBUG: PresentValue: \"$PresentValue\""
 
Old 09-23-2009, 08:06 PM   #5
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
lutusp:
Quote:
Did you read the post you are replying to?
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).
 
Old 09-23-2009, 08:46 PM   #6
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by musonio View Post
lutusp:

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:

PresentValue="$(xgamma 2>&1)"; echo "$PresentValue"

And please stop using backticks. It's unprofessional.
 
Old 09-23-2009, 09:17 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 554Reputation: 554Reputation: 554Reputation: 554Reputation: 554Reputation: 554
Quote:
Originally Posted by lutusp View Post
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:

PresentValue="$(xgamma 2>&1)"; echo "$PresentValue"

And please stop using backticks. It's unprofessional.
WOW! I have been screwing with this for the past hour! What a brain-burner :| I thought wtf is wrong here!?
Code:
hello="$(xgamma 2>&1)"
sh-4.0# echo "$hello"
-> Red 1.000, Green 1.000, Blue 1.000
sh-4.0#
Nice one, Paul

Last edited by GrapefruiTgirl; 09-23-2009 at 09:19 PM.
 
Old 09-24-2009, 03:17 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by musonio View Post
Catkin:
Thanks for your answer, but I am still stuck.
If I do:

Code:
echo "DEBUG: PresentValue: '$PresentValue'"
I get:

Code:
DEBUG: PresentValue: ''
Then the DEBUG is doing exactly what it should and identifying the problem -- $PresentValue is not being set.
 
Old 09-24-2009, 03:33 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by lutusp View Post
Code:
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 View Post
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.
 
Old 09-24-2009, 05:19 AM   #10
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
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).
 
Old 09-24-2009, 06:39 AM   #11
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by catkin View Post
In this situation = and == work identically
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 View Post
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 View Post
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 View Post
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.
 
Old 09-24-2009, 09:37 AM   #12
musonio
Member
 
Registered: Jun 2009
Distribution: PCLinuxOS
Posts: 33

Original Poster
Rep: Reputation: 15
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.
 
Old 09-24-2009, 09:47 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by lutusp View Post
Quote:
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.
See GNU Bash Reference
 
Old 09-25-2009, 12:28 AM   #14
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by catkin View Post
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.

See GNU Bash Reference
 
Old 09-25-2009, 12:44 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by lutusp View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple script to monitor POP3, SMTP service s3cr3t Solaris / OpenSolaris 1 05-29-2009 12:33 PM
toggle laptop for LCD projector theory_prof Linux - Laptop and Netbook 8 02-18-2009 03:57 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
Simple shell script to monitor network andybrr Programming 2 09-07-2006 11:12 AM
analog lcd monitor not working after installing a digital lcd monitor nishlq Linux - Hardware 6 08-18-2006 02:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration