:0
Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?
You must be signed in to comment.
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for:
echo $DISPLAY
:0.0
X=$(cat /sys/class/tty/tty0/active)
then you find all matching processesY=$(pgrep -t $X)
then you search each process's environment for DISPLAYgrep -z DISPLAY /proc/$Y/environ
That seems simple:for i in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do grep -z DISPLAY /proc/$i/environ; done | cut -d= -f2
:0 That's 111 characters rather than 243 and no ugly parsing of ps :-)for i in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do grep -z DISPLAY /proc/$i/environ; done | cut -d= -f2
grep: /proc/20358/environ: Permission denied :0DISPLAY So I propose this (164 chars):for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(grep -z DISPLAY= /proc/$p/environ | cut -d= -f2 2>/dev/null); [[ $d != "" ]] && break; done; echo $d
... do grep -z '^DISPLAY=' /proc/$p/environ 2> /dev/null; done | ...
Now we can drop that 'if' clause.for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do grep -z '^DISPLAY=' /proc/$p/environ 2>/dev/null; done | cut -d= -f2
Let me know if that works OK. I'm off to experiment with replacing grep/cut with sed :-)for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do grep -z '^DISPLAY=' /proc/$p/environ 2>/dev/null; done | cut -d= -f2
:0DISPLAY A better example without the cut:for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do grep -z '^DISPLAY=' /proc/$p/environ 2>/dev/null; done
DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0DISPLAY=:0 The loop goes ahead. That was the reason for the $d assignment and condition && break.for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do sed -nz '/^DISPLAY=/{s/.*=\(.*\)/\1\n/;p}' /proc/$p/environ 2>/dev/null; done
. Run sed on FILE. Don't print (-n) by default, the null-terminated (-z) lines. Search for REGEX, then run two commands on the matchessed -nz '/REGEX/{CMD1;CMD2}' FILE
. Match lines '^DISPLAY=' Replace 'DISPLAY=XYZ' with 'XYZ\n' Finally 'p' print the result.for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$p/environ 2>/dev/null); [[ $d != "" ]] && break; done; echo $d
awk is more clear to me than the slashes hell.for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$p/environ 2>/dev/null); [[ $d != "" ]] && break; done; echo $d
awk is more clear to me than the slashes hell.... [ -n "$d" ] && break
ps u | awk -v tty=$(cat /sys/class/tty/tty0/active) '$0 ~ tty {print $2}' | while read pid && [[ "$d" == "" ]]; do d="$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$pid/environ)"; if [ "$d" != "" ]; then echo "$d"; fi; done 2>/dev/null
Ended with 165:for p in $(pgrep -t $(cat /sys/class/tty/tty0/active)); do d=$(awk -v RS='\0' -F= '$1=="DISPLAY" {print $2}' /proc/$p/environ 2>/dev/null); [[ -n $d ]] && break; done; echo $d