Toggle screen resolution with a script, how?

How do I phrase a script that checks screen resolution with xrandr and changes resolution depending on what it is currently? Which is to say, toggles the resolution.

Sample output of xrandr -q

   1920x1080     59.98 +  59.97    59.96    59.93  
   1680x1050     59.95    59.88  
   1600x1024     60.17  
   1400x1050     59.98  
   1600x900      59.99*   59.94    59.95    59.82

How does it need to be phrased to check if the current resolution is 1600×900, then run xrandr -s 0, but if the current resolution is 1920×1080 run xrandr -s 4?

In short the question is how to make the script recognize the output of xrandr -q in order to toggle resolutions. For example:

#!/bin/bash

if <current resolution is 1920x1080>
then
  xrandr -s 4
elif <current resolution is 1600x900>
then
  xrandr -s 0
else
 <do nothing>
fi

Basically I need a means for the script to recognize which line the asterisk is on and switch it to the other one. How would that be accomplished?

Asked By: Jonathan Strange

||

Use process substitution:

#! /bin/bash

read resolution _rest < <(xrandr | grep '*')

if [[ $resolution = 1920x1080 ]] ; then
    xrandr -s 5
elif [[ $resolution = 1600x900 ]] ; then
    xrandr -s 0
else
    echo Unknown resolution >&2
fi
Answered By: choroba
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.