#!/bin/bash # Use /usr/local/bin/bash for Solaris systems # Use /bin/bash for Linux systems # # Name: list-update # Version: 0.2 # Created: 2007-05-30 # Last Updated: 2014-03-25 # Written By: Jim Cameron # # Location: # http://support.moonpoint.com/downloads/computer_languages/bash/list-update # # Description: Updates the specified mailing list # # Usage: list-update list # # List is a text file containing just the email address for list members. # It is created from a spreadsheet that contains member information. # The column containing the email addresses is saved as a text file, # which is the input for this script. Since many members have not provided # email addresses, there will be many blank lines in the original file. # # The filename specified on the command line will be replaced with the # results of the processes run by this script. # # Changes: version 0.2 added command to change file permissions to 644 if [ -z "$1" ] then echo "Usage $0 list" echo "" echo "List is the unprocessed mailing list text file, which will be" echo "updated. A backup file will be created with the same name, but" echo "with .bak added at the end of the filename" exit fi # Check for existence of file if [ ! -e "$1" ] then echo "Error! $1 does not exist" exit fi # Create a backup for the original file cp -p $1 $1.bak echo "Backup file created: $1.bak" echo "" # Convert the text file, which is in DOS format to Unix format. # DOS and Windows systems use CRLF at the end of lines, whereas # Unix systems use only LF. # # If the original file and the converted file are the same, # dos2unix will rewrite the original file after converting it. dos2unix $1 $1 # Display number of lines in original file echo -n "Number of lines in original file:" wc -l $1 echo "" # Remove blank lines cat $1 | sed -e '/^$/d' > $1.tmp # Sort file, storing results in original file sort -o $1 $1.tmp # Show duplicate entries echo "Duplicated Entries" uniq -d $1 echo "" # Remove duplicate entries uniq $1 $1.tmp # Check for invalid email addresses echo "Invalid Email Addresses" grep -v '@' $1.tmp grep -v "\." $1.tmp # Look for entries where the last character may have been omitted # A presumption is made that members won't have non-US country codes # as part of the FQDN, i.e., though co represents Columbia and ne # is the country code for Niger, no email address with .co or .ne # at the end of the address is expected. grep "co$" $1.tmp grep "or$" $1.tmp grep "ne$" $1.tmp echo "" # Put valid email addresses in original file grep '@' $1.tmp | grep "\." | grep -v "co$" | grep -v "or$" | grep -v "ne$" >$1 # Display number of valid, unique email addresses echo -n "Number of valid, unique email addresses:" wc -l $1 echo "" # Delete the temporary file rm $1.tmp # Set permissions to give owner r/w permission with group and world having # read permission chmod 644 $1 echo "$1 contains the updated list."