Skip to main content

How can I "lazily" read output from xrandr?

I have a bash script that I use to adjust my monitor brightness that uses xrandr --verbose to get the current brightness. It works fine, but using xrandr is kind of slow on my machine, as you can see here:

[PROMPT REDACTED]$ time xrandr --verbose
# xrandr output omitted for brevity
real    0m0.976s
user    0m0.003s
sys     0m0.002s

This outputs lots of information that I don't need, in addition to taking almost a full second. The only line out of the output that I actually need is Brightness: X. I am currently using this line to get the value from it:

BRIGHTNESS=`xrandr --verbose | grep -i brightness | cut -f2 -d ' ' | head -n1`

Side note: head is called at the end because I have 2 monitors, so I end up with 2 values, but only need 1, since I am keeping them both at the same brightness.

Since I only need that one line from xrandr --verbose, I was wondering if there is a way I could "lazily" evaluate it, by doing something like:

  • Stopping xrandr outputting once it reaches that line
  • Ignoring the rest of the output from xrandr once I have read that line
  • Something else?

I realize bash may not be the language best suited for this, so I am open to solutions in other languages as well.

gla3dr
  • 183
  • 1
  • 1
  • 9