Script to get List of logged in users

192 Views Asked by At

I'm writing a Shell script to get a list of logged in users, i want to show a list like this one :

The user AAA is on tty3
The user BBB is on pts/0

Here's what i've written so far :

echo "The user " | who | awk -F ' ' '{print $1}' | echo " is on " | who | awk -F ' ' '{print $2}'

but in only shows the second column, i get this :

tty3
pts/0
2

There are 2 best solutions below

0
On BEST ANSWER

Try doing this in single one awk statement:

who | awk '{print "The user " $1 " is on " $2}'

By doing that many piping, you are sending first one's input as another ones output. Hence you are losing previous data and at last only the output of who | awk '{print $2}' is coming out.

0
On

Last users

last | cut -d " " -f 1 | sort | uniq 

Last users detailed

last | uniq | column

who's logged in (username only)

who | cut -d' ' -f1 | sort | uniq

short who column

 who -su | sort | uniq | column