Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 47 lines (37 sloc) 1001 Bytes
#!/usr/bin/env bash
# pid2xid.sh
# Author: William Woodruff
# ------------------------
# Prints the X Window ID (xid) associated with a process ID.
# Requires GNU awk.
# ------------------------
# This code is licensed by William Woodruff under the MIT License.
# http://opensource.org/licenses/MIT
function usage {
echo "Usage: $(basename "${0}") <pid>"
exit 1
}
function fatal {
>&2 echo "Fatal: ${*}. Exiting."
exit 2
}
function installed {
cmd=$(command -v "${1}")
[[ -n "${cmd}" ]] && [[ -f "${cmd}" ]]
return ${?}
}
function pid2xid {
xids=($(xwininfo -int -children -root | sed -e '1,6d' | awk '{ print $1 }'))
for xid in "${xids[@]}"; do
[[ "${xid}" != [0-9]* ]] && continue
pid=$(xprop -id "${xid}" _NET_WM_PID | awk '{ print $3 }')
[[ "${pid}" != [0-9]* ]] && continue
if [[ "${pid}" -eq "${1}" ]]; then
echo "${xid}"
break
fi
done
}
installed gawk || fatal "Missing dependency: gawk (GNU awk)."
[[ -n "${1}" && -d "/proc/${1}" ]] || usage
pid2xid "${1}"