On Thu, 26 Oct 2000, Mark Shaw wrote:
> Is there anyway I can make a Alias in my /etc/aliases file that will
> email everyone with a certain GID???
You can modify and run the following program I wrote and circulated on the
Moongroup bash list. With a little tweaking, it should do what you want.
#!/bin/bash2
##
## Name:
## mailusers.sh
##
## Version:
## 0.98.17
##
## Purpose:
## Mail a message to all real users on your system.
##
## Usage:
## mailusers [-f <file>] [-i '<name1|name2|name3...>'] [-s '<subject>']
## mailusers [-t | -h]
##
## Options:
## -f = file containing the message body
## -i = list of users above UID 500 to ignore when parsing /etc/passwd
## -s = subject of email
## -t = test only; does not send any mail
## -h = show documentation
##
## Errorlevels:
## 0 = Success
## 1 = Failure
## 2 = Other
##
## Author:
## Todd A. Jacobs <[EMAIL PROTECTED]>
##
########################################################################
# Program Constants
# =-=-=-=-=-=-=-=-=
#
# This is a good place to store any values you pass to options -i, -s,
# or -f on a regular basis.
########################################################################
# Values of users to ignore above uid 500. We need the pipe as a field
# seperator, because we treat the list as a regex. This value will get
# APPENDED TO by the -i option.
_IGNORE='qmail|alias'
# Default subject. Can be overriden by the -s option.
_SUBJECT="Broadcast Message"
# Default text for message body. Can be overriden by the -f option.
Message_Body=/dev/null
########################################################################
# Program Functions
# =-=-=-=-=-=-=-=-=
#
# By storing complex operations as functions, we can simplify the flow
# of the main body of the script. This makes it more readable and more
# modular.
########################################################################
# 1. Export _IGNORE to the environment. Awk will only read the value if
# it is an environment variable.
# 2. Run awk with a field seperator of ':'
# 3. Check that the user ID is greater than or equal to 500, which is
# the lowest UID for mortal users under Red Hat Linux.
# 4. Check that the username doesn't match the regex stored in the
# variable _IGNORE.
# 5. Print the resulting matches from /etc/password
function GetUsers
{
export _IGNORE
awk -F: '$4 >= 500 && $1 !~ ENVIRON["_IGNORE"] { print $1 }' /etc/passwd
}
# Make sure options that need arguments have them, otherwise exit the
# script with an appropriate error message. We assume that any option
# passed without its necessary parameters is an error.
function NullArg
{
# We've been passed the name of the option first. We store it
# locally so that we can remove it from our argument list.
OptionName=$1
# We shift the option's name out of the way so we can test the
# value of $OPTARG as originally seen by the shell script, not
# the modified argument list passed into the function.
shift
# Let's see if the required arguments were passed. If the number
# of arguments is zero, then we weren't passed any arguments.
if [ $# -eq 0 ]
then
echo "Option -$OptionName requires a parameter."
exit 1
fi
}
# Excerpt the usage instructions from the internal documentation.
#
# '-A 2' tells grep to show the matching pattern plus the next two
# lines. The brackets around the U in '[U]sage' have the same effect as
# piping the output of grep through 'grep -v grep'. This is important,
# because we don't want to match these lines; just the ones in the
# documentation above. The sed script prettifies our output by stripping
# off the octathorps that start each line of documentation.
function ShowUsage
{
grep -A 2 [U]sage: $0 | sed 's/## *//'
exit 1
}
# Run mail agent such as /bin/mail.
#
# We protect against possible spaces in _SUBJECT by enclosing it in weak
# quotes.
function RunMail
{
for NAME in $_UserList
do
mail -s "$_SUBJECT" $NAME < $Message_Body
done
}
# If no options were passed on the original command-line, make sure that
# the hardcoded defaults are acceptable before proceeding.
function ConfirmDefaults
{
if [ $# -eq 0 ]
then
# Make sure we're using sane variables.
#
# If the variable already exists, it might interfere
# with the control flow of this function. By unsetting
# it, we're making sure that we're starting this
# function with a clean slate.
unset _YesNo
# Send summary of defaults to standard output.
echo "Using hardcoded defaults..."
echo -e "\tIgnoring regex \"$_IGNORE\""
echo -e "\tSubject is \"$_SUBJECT\""
echo -e "\tSending $Message_Body as the body of the message"
# Repeat prompt until user enters 'y' or 'n'.
#
# Since the program true always returns errorlevel 0,
# the only way out of this loop is the break command in
# the case statement below.
while true
do
# Send prompt to standard error. This is where
# prompts are supposed to go under Unix.
echo -n "Continue (y/n)? " >&2
# Get user input, and store it in a variable.
read _YesNo
# Allow only 'y' or 'n' as input.
#
# If _YesNo is null or doesn't exist, we'll
# assign it '0' so that the equality test
# doesn't choke on a variable that doesn't
# exist.
case ${_YesNo:-0} in
# If we like the defaults, we exit the
# current while loop and continue.
y) break
;;
# If we choose not to accept the
# defaults, show usage and exit.
n) echo
ShowUsage
exit 2
esac
# End 'while true' loop.
done
# Close 'if [ $# -eq 0 ]'
fi
# End function ConfirmDefaults.
}
########################################################################
# Process Options and Arguments
# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Now that we've defined our constants and our functions, we can begin
# doing the real work of the script. We'll start by processing our
# options and arguments. While you can actually run program code here
# (such as the actions performed by the -t and -h options), most often
# this section is for general housekeeping such as setting variables
# that will be needed later on in the program.
########################################################################
# Pass all arguments passed to the shell script to the ConfirmDefaults
# function. Without the $*, the function will only see its own calling
# arguments (e.g. null).
ConfirmDefaults $*
# Here's where the calling options (if any) are processed.
while getopts ":f:hi:s:t" opt
do
case $opt in
f)
NullArg f $OPTARG
# Make sure the file used for the message body
# really exists, is readable, and is not empty.
if [ -r $OPTARG -a -s $OPTARG ]
then
Message_Body=$OPTARG
else
echo Error: $OPTARG does not exist!
exit 1
fi
;;
h)
# Help
#
# If passed "-h" for help, use egrep to show the
# help comments designated by an inital ## and
# exit with errorlevel 2.
egrep '^##([ ]|$)' $0 | sed 's/## *//'
exit 2
;;
i)
# Ignore
#
# Append a pipe plus the values contained in
# -i's argument list to the hardcoded value of
# the _IGNORE variable.
NullArg i $OPTARG
_IGNORE="$_IGNORE|$OPTARG"
;;
s)
NullArg s $OPTARG
_SUBJECT="$OPTARG"
;;
t)
# Test Only
#
# Print list of user names that will be
# processed and exit without sending any mail.
# This option is useful to ensure you've set
# _IGNORE properly.
GetUsers
exit 2
;;
\?)
# Show proper usage if bad parameters are passed.
ShowUsage
# End 'case $opt'
esac
# End 'while getopts'
done
#########################################################################
# Main Program Begins Here
# =-=-=-=-=-=-=-=-=-=-=-=-
#
# Now that we've finished setting up, we can begin doing some real work
# by calling programs and defined functions. This section collects user
# names using function GetUsers, then mails them using function RunMail.
#########################################################################
# Stuff user names into a variable. xargs takes the multiline output
# from GetUsers and turns it into a single string which we then insert
# into _UserList.
_UserList=`GetUsers | xargs`
# Send out mail if there are names to process or report an error and
# exit.
#
# The -n test evaluates to false if the string contained in variable
# $_UserList is empty. We need to enclose the variable in weak quotes so
# that the whole variable is treated as a single string. Since the
# variable contains a list of space-seperated names, the shell will
# complain with an incomprehensible error if you don't do this.
if [ -n "$_UserList" ]
then
RunMail
else
echo "No names to process. Check your -i options or the _IGNORE constants."
exit 1
fi
# Report our success.
#
# The awk variable NF counts the number of fields (i.e. names) contained
# in $_UserList. We use awk instead of wc because wc does some funky
# formatting that we don't want here.
echo "Successfully sent $(echo $_UserList | awk '{ print NF }') emails."
--
Todd A. Jacobs
Senior Network Consultant
_______________________________________________
Redhat-list mailing list
[EMAIL PROTECTED]
https://listman.redhat.com/mailman/listinfo/redhat-list