LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook
User Name
Password
Linux - Laptop and Netbook Having a problem installing or configuring Linux on your laptop? Need help running Linux on your netbook? This forum is for you. This forum is for any topics relating to Linux and either traditional laptops or netbooks (such as the Asus EEE PC, Everex CloudBook or MSI Wind).

Notices


Reply
  Search this Thread
Old 03-06-2011, 01:30 PM   #1
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Rep: Reputation: 7
bash scripting, xrandr, and grep


There may be a much better why to do what I'm trying to do.

In the windoze world, if your plug your monitor into your laptop, the display magically jumps over to it. Unplug your monitor, and it's back to your laptop.

I've tried many things to do that using debian squeeze, but can't get it.

In high spirits, i decided to be a good little linux user and make a simple script. I thought.

Code:
#/bin/bash
if [ "$(xrandr -q) | $(grep 'VGA1 disconnected')" ]
then
echo "disconnected"
fi
This doesn't do anything at all. I have to C-c to end the script.
The ultimate goal is to make it so I can run the same script to set the monitor resolution or turn my lcd back on.

This is what I have in mind, written in C-ish style:
Code:
if ( $(xrandr -q) | $(grep 'VGA1 connected') )
   { $(xrandr --output VGA1 --mode 1280x1024 --output LVDS1 --off);}
else
   { $(xrandr --output VGA1 off --output LVDS1 --mode 1024x600); }
dont make too much fun of the C, i know it's not at all valid, but it makes a valid illustration!
 
Old 03-06-2011, 01:47 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
I would put the output of xrandr -q into a variable, along with the grep, like so:
Code:
var1=`xrandr -q | grep 'VGA1 disconnected'`
 
Old 03-06-2011, 02:28 PM   #3
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
Code:
#/bin/bash
noMonitor=`xrandr -q | grep 'VGA1 disconnected'`
if [ $noMonitor ]
then
xrandr --output VGA1 --off --output LVDS1 --mode 1024x600
else
xrandr --output VGA1 --mode 1280x1024 --output LVDS1 --off
fi
produces

[: 8: VGA1: unexpected operator
 
Old 03-06-2011, 02:31 PM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Code:
if [ -z "$noMonitor" ];
The -z test if $var1 is set or unset.
 
Old 03-06-2011, 09:48 PM   #5
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
i still have some complaining about unexpected operator VGA1 when monitor is disconnected. However, it does perform it's purpose.

Code:
#/bin/bash
Monitor=`xrandr -q | grep 'VGA1 disconnected'`
if  [ -z  $Monitor ]
then
echo Monitor detected.
$(xrandr --output VGA1 --mode 1280x1024 --output LVDS1 --off)
return 0
else
echo No monitor detected.
$(xrandr --output VGA1 --off --output LVDS1 --mode 1024x600)
return 0
fi
i think I'm going to do it in C, just for grins. If there is any ideas on why it still complains when monitor is disconnected, let me know!

I'll post the C code, just for viewing pleasure
 
Old 03-06-2011, 11:16 PM   #6
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
Code:
#include <cstdlib>

int main(){
        using namespace std;
        bool bMonitor = system("xrandr -q | grep 'VGA1 disconnected'");
        if ( bMonitor ){
                system("xrandr --output VGA1 --mode 1280x1024 --output LVDS1 --off");
        }
        else{
                system("xrandr --output VGA1 --off --output LVDS1 --mode 1024x600");
        }

        return 0;
}
I dont know if I should be proud, ashamed, or annoyed. This took just a minute, and works perfectly.

What kind of idiot can write a cpp to do this, but cant get a bash script to do it? All well!

Note to viewers: this program does raise security issues, because it assumes xrandr and screen have not been compromised. If anybody wants this for their system, let me know and I'll add a little more to it to make it safer.
 
Old 03-06-2011, 11:32 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
You may be going about it the wrong way. I suggest looking into halevt, which uses hal to search for specified system events such as adding and removing hardware and runs arbitrary commands when it detects them.
 
Old 03-07-2011, 07:47 AM   #8
comp_brad1136
Member
 
Registered: Nov 2004
Location: SW Missouri, USA
Distribution: Debian 6 "Squeeze"
Posts: 55

Original Poster
Rep: Reputation: 7
i ran $ halevt -i to see what happens. It performs as advertised when i unplugged my mouse and plugged it back in. But, it didn't do that with my monitor. It's a nifty daemon though, and I won't forget it's existance. Thank you for the suggestion!
 
  


Reply



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
scripting: how to grep either a or b? simransab Linux - Newbie 7 10-14-2009 03:30 AM
Bash scripting question with find and grep and .bashrc - a multi year problem brfindla Linux - Software 9 08-24-2009 09:54 AM
radeon and dual head, to xrandr or not to xrandr, that's the question i92guboj Linux - Desktop 1 06-17-2009 05:48 AM
perl scripting using grep gazman1 Programming 9 06-09-2006 01:49 PM
line addressing with grep (shell scripting) j2dizzo Linux - General 13 03-03-2004 09:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook

All times are GMT -5. The time now is 05:43 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