writing a script to mail all users online

1

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

Posted 2014-07-14T02:45:27.603

Reputation: 11

Answers

0

@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
do
  echo "Hi there!" | mail $user
done

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, ...

tink

Posted 2014-07-14T02:45:27.603

Reputation: 1 419

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 – 2014-07-14T11:54:17.280