I'm trying to print a list of users, who are currently logged on a terminal. It should look like this:
I only got this so far:
I'm missing the Terminal and the Login time. How can I display them? This is what I got so far:
#!/bin/bash
NOWDATE=$(date +"%Y-%m-%d")
NOWTIME=$(date +"%T")
USERS=$(who | cut -d " " -f1)
TERMINAL=0
LOGIN=0
for u in $USERS
do
echo "$NOWDATE""_""$NOWTIME User: " $u
done
Best Answer
Parsing the output of
w
is probably a better approach thanwho
. Here are some representative data, which shows the login time:Those are more widely available than
finger
. Since this is a classroom exercise, parsing the data is left to OP. As a hint, awk can do more than print its fields in a one-liner:w
by having in the awk script aBEGIN
section (to set a line-number or state).USER
.awk
will quit when there is no more data; it is not necessary to know the number of users to do this.If OP is told to use
who
, that has options to list more information, e.g.,again, showing the terminal name and the login times.