bash - finding the tty of a logged in user -
i'm trying find tty
number of person logging in using loops. code far is:
if [ "$#" -ne 1 ] echo "usage: mon user" exit 1 fi user="$1" # until | grep "^$user " > /dev/null tty=$(who | cut -c 9-13) sleep 60 done echo "$user has logged onto $tty"
how go cutting out characters between 9-13 in first row? secondly, did correctly? can't test without having person work with.
you can reduce code lot loops not required seach extract user
, tty
for example
if [ "$#" -ne 1 ] echo "usage: mon user" exit 1 fi tty=$(who | grep "$user" | awk '{print $2}') echo "$user has logged onto $tty"
what does?
who
lists logged in usersgrep "$user"
searches lines containing$user
awk '{print $2}'
awk prints second column in output
you can use cut
instead of awk
in program. instead of using -c
option selects characters use -f
option select fields
that like
| grep "$user" | cut -d\ -f 2
-d\
sets delimtter space-f 2
selects second field output
Comments
Post a Comment