
ttyowner
By:
bash-masters on
Sep 5th, 2012 | syntax:
Bash | size: 0.80 KB | hits: 192 | expires: Never
#!/bin/bash
#
# Original Author: Triston J. Taylor pc.wiz.tt@gmail.com
# Script Name: ttyowner;
# Domain: Unix/Linux
# Verified in: Ubuntu, gentoo, mint, and SuSe
# Keywords: unix linux bash real user-name sudo su tty pty
#
# This script finds out who is really logged in to the tty.
# Usually called by an administration script.
# Avoid being sourced as
return 1 2>&-;
title="${0##*/}";
function usage() {
echo "$title -- Display the user name of the tty owner";
echo "usage: $title";
}
[[ $1 =~ ^(--help|--usage)$ ]] && {
usage;
exit;
} || {
[[ $# -ne 0 ]] && {
usage >&2;
exit 1;
}
}
# Must be a tty.
tty -s && {
set -- $(ps -ao user,tty,ucmd | grep 'ps');
set -- $(who | grep $2); echo $1
}