I am taking my first Linux class and we are writing scripts. Currently I'm trying to write a script that will look at who is online and then send them a mail

This is an entry level linux class so nothing to crazy, and I'm an Old man taking this class. I haven't used computers since I had my TRS-80 and learned basic.

What I'm wondering is how I store user names in some sort of array or variable. I think I need to start with a variable counting the users like

NUMUSERS=users | wc -w

and then I think I need to use that info to create a variable that will grep all the names.

USERNAME=`who | awk '{print $1}' | sort | uniq

this works if only one person is online. But I'm not sure how/what to write if more people get on.

Any help?


@DamianOgre: I think you're over-thinking this. You don't need to count them, just loop over the returned names.

who | cut -d " " -f 1| sort -u |  while read user
  echo "Hi there!" | mail $user

P.S.: I LOVE awk, but here cut will do just fine. :) P.P.S.: Some commands may differ in your set-up, it might be mailx, mutt, ...

  • And if you really need to count them, just drop a x=0 at the beginning and a (( x += 1 )) in the while-do loop. – bgStack15 Jul 14 '14 at 11:54

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.