I'm trying to print a list of users, who are currently logged on a terminal. It should look like this:

enter image description here

I only got this so far:

enter image description here

I'm missing the Terminal and the Login time. How can I display them? This is what I got so far:

NOWDATE=$(date +"%Y-%m-%d")
NOWTIME=$(date +"%T")
USERS=$(who | cut -d " " -f1)

for u in $USERS
    echo "$NOWDATE""_""$NOWTIME User: " $u
  • it would be more interesting you doing your class homework to learn – Rui F Ribeiro Nov 15 '15 at 11:38
  • Agreed. Then OP would learn how to answer questions. – Thomas Dickey Nov 15 '15 at 11:46
  • hints: cut is a good idea, you could use it for other columns too. In alternative, awk but I suspect it is a more advanced subject that will come up later on – Rui F Ribeiro Nov 15 '15 at 12:01
  • @RuiFRibeiro I've used awk and it's working fine USERS=$(who | awk '{print $1" Terminal: "$2" Login: "$3"}' but I don't know how I can display the output in a seperate row like above – Rudi Nov 15 '15 at 12:33
  • @RuiFRibeiro this is how it looks like when I want to give out $u of $USERS in a for-loop: fs5.directupload.net/images/151115/f29j4on4.png I've used the awk-command like this: USERS=$(who | awk '{print "User: "$1" Terminal: "$2" Login: "$3"}' – Rudi Nov 15 '15 at 12:43

Parsing the output of w is probably a better approach than who. Here are some representative data, which shows the login time:

$ who
tom      pts/1        2015-11-15 06:39 (michener:S.0)
$ w
 06:40:10 up  1:04,  1 user,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
tom      pts/1    michener:S.0     06:39    2.00s  0.03s  0.00s w

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:

  • Typically, one would handle the output of w by having in the awk script a BEGIN section (to set a line-number or state).
  • Then, a default action for each line (just curly braces with no pattern) would increment the line number.
  • Using the line number, handle the first line specially (skip it in this case: OP may need the number of users for a report header, but that is not used in OP's example), and skip the line with USER.
  • After that, each line can be printed as OP needs. 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.,

$ who -l -u
LOGIN    tty5         2015-11-15 05:36              3670 id=5
LOGIN    tty6         2015-11-15 05:36              3671 id=6
LOGIN    tty4         2015-11-15 05:36              3669 id=4
LOGIN    tty3         2015-11-15 05:36              3668 id=3
LOGIN    tty2         2015-11-15 05:36              3667 id=2
LOGIN    tty1         2015-11-15 05:36              3666 id=1
tom      pts/1        2015-11-15 06:39 00:06        5780 (michener:S.0)
tom      pts/2        2015-11-15 06:52   .          6078 (michener:S.1)

again, showing the terminal name and the login times.

| improve this answer | |
  • My only problem is only to get the "string" into a seperate row. I've edited my variable USERS and it looks like this now: USERS=$(who | awk '{print $1" Terminal: "$2" Login: "$3"}'). My only problem is that when I loop the $USERS variable, it's not printing the 3 paramteres in one row... Every parameter has their own rows, so I'm getting in total 10 rows (with 2 users logged in) and not just 2 rows... – Rudi Nov 15 '15 at 12:05

No need of bash script. you can use finger command. It will give complete information about all login users.

Just open your terminal and type finger. you will get what you need. If you want to know more about what finger can do read here

| improve this answer | |
  • he is asking how to parse the who command and it look like it is a class assignment afaik – Rui F Ribeiro Nov 15 '15 at 11:39
  • @RuiFRibeiro still finger command helps him , let me modify my answer with awk help – rɑːdʒɑ Nov 15 '15 at 11:41
  • finger is not available anymore by default in most modern systems, and even in the time it was, I always disabled it for security reasons. I do prefer much more following the w suggestion. – Rui F Ribeiro Nov 15 '15 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.