#! / bin / csh
#
# In some cases, need to e-mail sent to all users on the system.
# Alerts the user when the hardware upgrade of the information is a good example.
# The messages were sent to each user is a waste of time,
# this is the invention of e-mail aliases and mailing list of a task.
# But the recording system on the mailing list for all users is a problem,
# if the lazy update mailing list, then add and remove
# Users on the most accurate; If there are many users,
# then the other it is difficult to control the size of the list.
#
# The following script called mailfile, to solve these problems provides a simple way.
# It can be directly from the / etc / passwd file access login name and e-mail sent to all users.
#
# Mailfile: This script mails the specified file to all users
# Of the system. It skips the first 17 accounts so
# We do not send the email to system accounts like
# ‘Root’.
#
# USAGE: mailfile “Subject goes here” filename.txt
#
# Check for asubject
#
if ( `echo $ 1 | awk ‘(print $ 1)’` == “”) then
echo You did not supply a subject for the message.
echo Be sure to enclose it in quotes.
exit 1
else
# Get the subject of the message
set subject = $ 1
endif
#
# Check for a filename
#
if ($ 2 == “”) then
echo You did not supply a file name.
exit 2
else
# Get the name of hte file to send
set filename = $ 2
endif
#
# Check that the ifle exists
#
if (-f $ filename) then
echo Sending file $ filename
else
echo File does not exist.
exit 3
endif
#
# Loop through every login name, but skip the first 17 accounts
#
foreach user ( `awk-F: ‘(print $ 1)’ / etc / passwd | tail +17`)
# Mail the file
echo Mailing to $ user
mail-s “$ subject” $ user <$ filename
# Sleep for a few seconds so we don; t overload the mailer
# On fast systems or systems with few accounts, you can
# Probably take this delay out.
sleep 2
end
The script receives two parameters, first one is the subject of the message, which is included in quotation marks; second one is to send a message that contains the text of the file name. Therefore, it is necessary to e-mail sent to all users, warning users to upgrade server hardware and ordered as follows:
mailfile “System upgrade at 5: pm” upgrade.txt
file contains upgrade.txt send text messages to all users. This method is where the real purpose of the text file can be saved and a simple modification, the next upgrade system can be re-sent it.
Tip: If the user login to use text-based rather than graphical login, you can add to the e-mail / etc / motd file, and so that they can reach the user. The document will be any text after the user logs in and before the appearance of a shell prompt, displayed in each user’s screen.




