LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-29-2014, 12:03 AM   #1
r_avital
LQ Newbie
 
Registered: Oct 2006
Posts: 26

Rep: Reputation: 15
Reading output into a script variable


Hi all,
I've been able to make progress on my own but I'm stuck. This is what I have in a bash script:
Code:
res="$(xrandr -q|grep \"*\")"

if [[ "${res:1:1}" != 0 ]]; then
        echo "something"
fi
This echoes "something" to the terminal screen. I found it in another thread here. What I'm trying to do, is read the current resolution, and act on it depending on what the resolution is. so, "xrandr -q|grep"*" in a terminal gives me the following:

" 1600x1200 60.0*+" -- without the quotes but WITH the leading spaces.

Of all the lines returned by xrandr -q, only one will have the asterisk in it (showing the current resolution). This is a laptop that will either be connected to an external monitor (1600x1200 resolution) or not (1400x1050 resolution).

How can I evaluate the returned value in an "if" statement? What is the meaning of the :1:1 in the if statement above? Is this some sort of array index?

Thanks in advance
 
Old 06-29-2014, 03:54 AM   #2
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
The backslashes seem to be the problem. You want to evaluate the first non-white text, so this works:
Code:
#!/bin/bash
set $(echo $(xrandr -q|grep "*"))
case $1 in
  1600x1200) echo yeah ;;
  *) echo no ;;
esac
Using echo without double-quotes eliminates leading white-space and reduces tabs/multi-spaces to single spaces.
 
1 members found this post helpful.
Old 06-29-2014, 06:10 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,692

Rep: Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275Reputation: 7275
set -- $(xrandr -q|grep "*")
is enough, do not need that $(echo ...)

about ${res:1:1} see man bash, parameter expansion, substring
 
2 members found this post helpful.
Old 06-30-2014, 12:24 AM   #4
r_avital
LQ Newbie
 
Registered: Oct 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks so much, both of you, and yes, the discussion on parameter expansion and substring is very useful.

Here's what I ended up with:

in /etc/lightdm/lightdm.conf.d I have a file simply named "my.conf" which has the following:
Code:
[SeatDefaults]
greeter-setup-script=/etc/lightdm/lightdmxrandr.sh
This executes when the lightdm (or other) greeter starts, and after X started.

One directory up from than, in /etc/lightdm, I have the script named lightdmxrandr.sh:
Code:
#!/bin/sh
set $(xrandr -q|grep "*")
case $1 in
  1600x1200) xrandr -o left ;;
  *) ;;
esac
So, when the laptop boots up, xrandr -q knows which resolution it is running with (Connected to external monitor = 1600x1200, not connected = 1920x1080). My external monitor is rotated vertically, so lightdm knows whether or not to rotate itself left, thanks to this script.

Tested, works beautifully. if you see anything in my bash code that is wrong, I'd appreciate the feedback (I'm more of an OO programmer -- java, PowerBuilder -- and SQL programmer, I have a lot to learn about bash scripting).

Either way, thanks so much both of you!
 
  


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
[SOLVED] script output name from a variable darkego Linux - Newbie 4 07-10-2012 10:12 AM
Reading comma separated variable into other variables in shell script suryaemlinux Programming 5 12-22-2010 07:38 PM
reading command output by line in Bourne script? ocicat Programming 6 02-14-2010 02:48 AM
Saving a text file as a variable and reading it every second in java script mrobertson Programming 4 03-26-2007 08:25 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:13 AM.

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