LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Reading output into a script variable (https://www.linuxquestions.org/questions/programming-9/reading-output-into-a-script-variable-4175509552/)

r_avital 06-29-2014 12:03 AM

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 :)

gnashley 06-29-2014 03:54 AM

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.

pan64 06-29-2014 06:10 AM

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

about ${res:1:1} see man bash, parameter expansion, substring

r_avital 06-30-2014 12:24 AM

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! :)


All times are GMT -5. The time now is 12:16 AM.