How can I "lazily" read output from xrandr?
You can use awk
to remove some pipelines (processes) and to only read the file until encountering the first instance of Brightness:
xrandr --verbose | awk '/Brightness/ { print $2; exit }'
Let's try and stop and ignore upon the first find of brigthness
. From grep
man page:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines.
This is my final version. Note that we don't even need the head
:
BRIGHTNESS=`xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '`
In addition to @LatinSuD's suggestion of using grep
's -m
flag to stop reading after a match, you can adjust the size of xrandr
's stdout buffer with a tool like stdbuf
like so:
BRIGHTNESS=`stdbuf -o0 xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '`
This can give you a significant speed increase:
$ cat brightness
xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '
$ time sh brightness > /dev/null
sh brightness > /dev/null 0.00s user 0.00s system 1% cpu 0.485 total
$ cat brightness_nobuffer
stdbuf -o0 xrandr --verbose | grep -m 1 -i brightness | cut -f2 -d ' '
[ para ~ . ]$ time sh brightness_nobuffer > /dev/null
sh brightness_nobuffer > /dev/null 0.01s user 0.01s system 10% cpu 0.130 total