Isn't there a race / hang?
The example calls clipnotify and, when it returns, does something with the new selection value. But what if a change arrives just before clipnotify starts listening? Then the program will wait forever and not notice the new selection value, right?
I understand it may be impossible to catch brief transient values, but I'd like eventual consistency; e.g. if the selection value changes to "foo" and stays that way, the script should eventually notice that.
To demonstrate:
In bash shell window #1, run this clipnotify loop:
while ./clipnotify; do echo "PRIMARY: '$(xclip -o -sel p)' CLIPBOARD: '$(xclip -o -sel c)'"; done
In bash shell window #2, run this naive polling loop:
p0=$(xclip -o -sel p); c0=$(xclip -o -sel c); while true; do p1=$(xclip -o -sel p); c1=$(xclip -o -sel c); if [[ "$p1" != "$p0" || "$c1" != "$c0" ]]; then echo "PRIMARY: '$p1' CLIPBOARD: '$c1'"; p0=$p1; c0=$c1; fi; done
In bash shell window #3, run this repeatedly:
echo -n foo | xclip; echo -n bar | xclip
Desired result: the last line, in both windows #1 and #2, should always look like:
...
PRIMARY: 'bar' CLIPBOARD: 'whatever'
Actual result: window #2 (the naive polling loop) is always good. But window #1 (the clipnotify loop) sometimes ends up showing the not-most-recent value:
...
PRIMARY: 'foo' CLIPBOARD: 'whatever'