Advertisement
bash-masters

ttyowner

Sep 5th, 2012
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.80 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Original Author: Triston J. Taylor pc.wiz.tt@gmail.com
  4. # Script Name: ttyowner;
  5. # Domain: Unix/Linux
  6. # Verified in: Ubuntu, gentoo, mint, and SuSe
  7. # Keywords: unix linux bash real user-name sudo su tty pty
  8. #
  9. # This script finds out who is really logged in to the tty.
  10. # Usually called by an administration script.
  11.  
  12. # Avoid being sourced as
  13. return 1 2>&-;
  14.  
  15. title="${0##*/}";
  16.  
  17. function usage() {
  18.     echo "$title -- Display the user name of the tty owner";
  19.     echo "usage: $title";    
  20. }
  21.  
  22. [[ $1 =~ ^(--help|--usage)$ ]] && {
  23.  
  24.     usage;
  25.     exit;
  26.    
  27. } || {
  28.  
  29.     [[ $# -ne 0 ]] && {
  30.    
  31.         usage >&2;
  32.         exit 1;
  33.        
  34.     }
  35.    
  36. }
  37.  
  38. # Must be a tty.
  39. tty -s && {
  40.     set -- $(ps -ao user,tty,ucmd | grep 'ps');
  41.     set -- $(who | grep $2); echo $1
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement