0

I need a little help to finish off a little code. I want to do a query with the who command, but I only want to see the users listed I give in arguments with username and pts/num, ordered by the time of login. It works for 1 user, but I would like to get it work with more users. Here it is atm:

#!/bin/bash
who |
awk '($1 ~ /'$*'/){print $3 $4" "$1" "$2}' |
sort -n |
awk '{ print $2, $3 }'

How can I make it work with multiple users?

Sample use:

./script username1 username2 blabla pete stevie
8
  • give an example how do you want to run it Commented May 13, 2014 at 22:01
  • ./script username1 username2 blabla pete stevie etc Commented May 13, 2014 at 22:02
  • another homework ? :) Commented May 13, 2014 at 22:02
  • I'm surprised that ...awk '($1 ~ /'${*// /\|/}'){... doesn't work. At least in bash 3.2. My intent is to globally sub spaces with '|' chars, so the arg-list would be converted to an awk OR able expression, like /username1|username2|blabla|.../. Can any bash-ites say if this is possible? Hm... doesn't work in RH ksh either. Good luck to all. Commented May 13, 2014 at 22:38
  • 1
    umm thank you Jonathan Leffler :) Commented May 13, 2014 at 23:36

1 Answer 1

1

If I get you correct than for:

$ who
oxo    tty7         2014-05-12 14:32 (:0)
bar    pts/5        2014-05-12 18:35 (:0:S.1)
oxo    pts/1        2014-05-13 13:29 (:0:S.5)
baz    pts/8        2014-05-12 18:35 (:0:S.2)
oxo    pts/12       2014-05-12 18:35 (:0:S.3)
oxo    pts/13       2014-05-12 18:35 (:0:S.4)
foo    pts/15       2014-05-12 18:35 (:0:S.0)
bar    pts/17       2014-05-13 19:36 (:0:S.6)
bar    pts/18       2014-05-14 00:03 (:0:S.7)

you expect to get for example:

$ ./who.sh foo bar
bar pts/5
foo pts/15
bar pts/17
bar pts/18

If so than this will work for you:

#!/bin/bash    
users=`echo $@|tr " " "|"`
who|sort -k 3|awk -v users="$users" '$1 ~ users {print $1" "$2}'

Actually it could be done just in the awk witout any tr or sort but I hope it's good enough.

UPDATE:

To get rid of tr this can be used:

#!/bin/bash
who|sort -k 3|awk -v users="$*" 'BEGIN { regex = gensub(/\s/, "|" ,"g", users) }; $1 ~ regex { print $1" "$2 }'

You can use asorti() to replace sort.

Sign up to request clarification or add additional context in comments.

10 Comments

what does the -k 3 on the sort? if it sorts by the 3rd column (yyyy-mm-dd), the 4th column (hh:mm) should be recognized as well. what if 2 users logged in the same day?
neaaaat, im testing on more boxes, on different who outputs. but who do you put | between the arguments?
there is a restricition by the teacher, the script should not create any files
i still dont get the gensub, what do you replace there?
but what does it do exactly? I dont get it why is it needed.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.