LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2013, 09:49 PM   #1
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Rep: Reputation: Disabled
Bash blues... Need help with simple script to control xrandr LVDS settings


Need to be able to turn off my laptop screen with a one key toggle. This does not involve the use of an external VGA or other monitor. Running openbox on Navigatrix (Debian/Ubuntu based "Boat-Centric distro) Machine is an older Dell Studio 1535.

Due to power usage (onboard a vessel) I need to be able to kill the built in display while leaving the box running. First solution was to keybind a shell command to enable or disable the display Eg: Super-N runs the command 'xrandr --output LVDS --off' killing thto e display. Super-M runs the command 'xrandr --output LVDS --auto restoring the display to its "prefered" settings.

Works well enough but being bone-headed I want to be able to toggle on/off with one key. Figured a bash script would work as I found numerous examples to enable/disable LVDS when using external monitors. Problem is I have been unable to bend any of them to my will, probably since I just know enough to be dangerous. Hence this great wall of text hoping someone will take pity and put me out of my misery. Short (as I can make it) description of my method of attack, and where I fell on my butt.

1. Looked at output of shell command "xrandr | grep "LVDS" with LVDS output enabled and disabled. These are the results:
Code:
LVDS ENABLED

david@wilson:~$ xrandr | grep "LVDS" 
LVDS connected 1280x800+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
david@wilson:~$

LVDS DISABLED

david@wilson:~$ xrandr | grep "LVDS" 
LVDS connected  (normal left inverted right x axis y axis) 
david@wilson:~$
2. Looked pretty simple to me.. The string "1280x800+0+0" is missing when LVDS is disabled. Hence the script logic is:
Code:
Variable = "1280x800+0+0"
Run the shell command "xrandr | grep "LVDS"
If the Variable is not present (we know LVDS is disabled) 
Then run the command "xrandr --output LVDS --auto"
Else (variable is present) run command "xrandr --output LVDS --off"
Finished
Keybind the script and it should turn the screen on if its off, and off if its on with one key.... Problem is I lost my way amongst the operators and syntax and , and, and, just to much to take in in one day, One attempt that did not work.
Code:
#!/bin/bash

INTERNAL_OUTPUT="1280x800+0+0"

xrandr | grep "LVDS"

if [[ -z $INTERNAL_OUTPUT ]]
 then
    xrandr --output LVDS --auto 
else
    xrandr --output LVDS --off
fi
All this script will do is output "xrandr | grep "LVDS"

End of whine... Time for a rum and a nap whilest I await rescue.

TIA
 
Old 09-05-2013, 10:15 PM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
nearly

a few way to do it, I'll start with the long hand

Code:
#!/bin/bash

INTERNAL_OUTPUT="LVDS.*1280x800+0+0"

xrandr | grep -q "$INTERNAL_OUTPUT"

if [[ "$?" -ne "0" ]]
 then
    xrandr --output LVDS --auto 
else
    xrandr --output LVDS --off
fi
$? is the exit code last command, in this case grep.

grep will give exit 0 if pattern matches, and 1 if not

a few other things we can do
grep -q will be silent ( no output )

we can also use another way of writing the test/action out

http://www.tldp.org/LDP/abs/html/tes...ucts.html#EX11

Code:
#!/bin/bash

INTERNAL_OUTPUT="LVDS.*1280x800+0+0"
xrandr | grep -q "$INTERNAL_OUTPUT" && xrandr --output LVDS --off \
                                    || xrandr --output LVDS --auto
here we do --off if grep is true ( 0 ) and --auto when false ( none zero )

in both the above I have expanded the grep a little

grep is looking for lines which contain

LVDS(any character)(repeated one or more times)1280x800+0+0

LVDS*1280x800+0+0
wouldn't work, since grep is looking for LVDSS, or LDVSSSSSSSSSSSSSSSSS
. is special, as it can mean any char

hope that makes sense

Last edited by Firerat; 09-05-2013 at 10:16 PM.
 
1 members found this post helpful.
Old 09-06-2013, 07:16 AM   #3
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Original Poster
Rep: Reputation: Disabled
Feel like R. Crusoe when Friday showed up... Tks for the solution.
However, while the second version of the script worked exactly as advertised, the first version when run from the command line with LVDS enabled only returns the grep output
Code:
david@wilson:~$ ~/.scripts/screen-toggle
LVDS connected 1280x800+0+0 (normal left inverted right x axis y axis) 331mm x 207mm
david@wilson:~$
If however the script is run with LVDS disabled, it will re-enable it no problem ???? Curious as to your opinion why this behaviour purely for educational purposes.

Like the elegance of the second solution it actually made sense even before reading your included link.

TKS for the help
 
Old 09-06-2013, 08:45 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Sorry, you have me stumped...
[Edit] Very stumped,.. the first also uses grep -q
This means grep will not produce any output, from grep --help
Code:
-q, --quiet, --silent     suppress all normal output
[/Edit]

seems to work here
technically the if should be (( )) instead of [[ ]], [[]] is normally for strings and (()) maths, but the -ne should make it math

http://tldp.org/LDP/abs/html/comparison-ops.html



actually, I posted the wrong link
http://www.tldp.org/LDP/abs/html/lis...ml#LISTCONSREF

better explains it


it's great for the command line, as you can chain many commands and not have to use if [ .. ]; then ...

for instance
Code:
./configure && make
is better than
Code:
./configure; if [[ "$?" -eq "0" ]]; then make;fi

Last edited by Firerat; 09-06-2013 at 08:50 AM.
 
Old 09-06-2013, 09:39 AM   #5
captdavid1949
LQ Newbie
 
Registered: Sep 2012
Location: Deltaville VA
Distribution: Navigatrix
Posts: 21

Original Poster
Rep: Reputation: Disabled
I will second the "very stumped" had the same kind of problems with my earlier (feeble) attempts. Thanks for the links, interesting stuff and maybe I will remember where to look the next time I have to Bash an issue. Glad to say script 2 works like a charm.
 
  


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
How to get some bash scripts into a simple bash script with some echo and if statement. y0_gesh Programming 3 03-01-2012 10:46 AM
Simple bash script please help! Cheloo Linux - General 3 07-13-2008 04:54 PM
Simple Bash Script Filipe Linux - Newbie 2 07-17-2007 10:07 AM
LXer: Simple script restores your system settings after OS reinstall LXer Syndicated Linux News 0 06-20-2007 10:17 PM
control Firefox proxy settings with a script xylex_blaiste Programming 1 02-21-2007 09:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration