1

I have a shell script, that loops through xrandr to find connected monitors, which get assigned inside of a variable. Inside that loop, I want to have another loop that determines the width of said connected monitors.

How can I do this?

0

You can just post process the results of the xrandr using any of your favorite tools like grep, awk, etc.

$ xrandr --query
Screen 0: minimum 320 x 200, current 1366 x 768, maximum 8192 x 8192
LVDS-1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
   1366x768      60.04*+
   1360x768      59.80    59.96  
VGA-1 disconnected (normal left inverted right x axis y axis)
SVIDEO-1 disconnected (normal left inverted right x axis y axis)

You can get the "connected" screens like :

$ xrandr --query |grep -w 'connected'
LVDS-1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm
#With grep , the use -w means grep for a word.

Or with awk:

$ xrandr --query |awk '/\<connected\>/{print}'
LVDS-1 connected 1366x768+0+0 (normal left inverted right x axis y axis) 344mm x 193mm

With awk you can also print various fields from the above output (awk default fields delimiter is white space):

$ xrandr --query |awk '/\<connected\>/{print $1}'
LVDS-1
$ xrandr --query |awk '/\<connected\>/{print $3}'
1366x768+0+0

Or you can even ask awk to use a customized fields delimiter like space, x and +:

$ xrandr --query |awk -F '[ x+]' '/\<connected\>/{print $1,$3,$4}'
LVDS-1 1366 768

Then the most easy way to process all your connected screens is to use an array :

$ IFS=$'\n' screens=( $(xrandr --query |awk -F '[ x+]' '/\<connected\>/{print $1,$3,$4}') )
$ for screen in "${screens[@]}";do
> echo "$screen" # or do whatever action you want to do with each screen
> done
LVDS-1 1366 768

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.