#!/bin/bash attachmentSpecified=false numAttachments=0 numRecipients=0 while true do echo -n 'Specify sender email address: ' # Retrieve the sender's email address from input read senderEmail shopt -s nocasematch # Check against test@example.com email format if [[ "$senderEmail" =~ [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4} ]]; then echo "$senderEmail has been added" break else echo "Invalid sender email address specified: $senderEmail" fi done while true do echo -n 'Specify recipient email address: ' # Retrieve the recipient's email address from input read recipientEmail shopt -s nocasematch # Check against test@example.com email format if [[ "$recipientEmail" =~ [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4} ]]; then # Increment the numRecipients counter by 1 numRecipients=$((numRecipients+1)) # Add the recipient email address to the array recipientArr[$numRecipients]=$recipientEmail # Tell the user the recipient's email address has been added echo "$recipientEmail has been added" while true do echo -n 'Add another recipient? (y|n) ' read ltr1 rest case ${ltr1} in [Yy]) break;; # Do not break out of the loop [Nn]) break;; # Break out of the loop *) echo; echo Unrecognized choice: ${ltr1} ;; esac done else echo "Invalid recipient email address specified: $recipientEmail" fi case ${ltr1} in [Nn]) break;; # Break out of the loop esac done echo -n 'Specify email subject: ' # Retrieve the email subject from input read subject while true do echo -n 'Add an attachment? (y|n) ' # Retrieve user's response and save as ltr read ltr2 rest case ${ltr2} in [Yy]) while true do echo -n 'Specify a directory that contains the attachments: ' read directory if [ -d "$directory" ]; then # User specified a valid directory. Break out of the loop break else # User specified an invalid directory. Show an error message and prompt again echo "$directory is not a directory" fi done cd $directory echo "$directory contains the following files:" ls -l | grep ^- | awk '{print $9}' while true do echo -n 'Specify the name of an attachment you want to include: ' read attachment if [ -f $attachment ]; then # Increment the numAttachments counter by 1 numAttachments=$((numAttachments+1)) # Add the attachment name to the array attachmentArr[$numAttachments]=$attachment # Tell the user the attachment has been added echo "The $attachment has been added" while true do echo -n 'Do you want to add another attachment? (y|n) ' # Retrieve user's response and save as ltr3 read ltr3 rest case ${ltr3} in [Yy]) break;; # Do not break out of the loop [Nn]) break;; # Break out of the loop *) echo; echo Unrecognized choice: ${ltr3} ;; esac done else echo "File $attachment does not exist." fi case ${ltr3} in [Nn]) break;; # Break out of the loop esac done while true do echo -n 'Enter your message: ' read message if [ "$message" == "" ] then echo -n 'Are you sure you do not want to enter a message? (y|n) ' # Retrieve user's response and save as ltr4 read ltr4 rest case ${ltr4} in [Yy]) break;; # Proceed to next step [Nn]) ;; # Stay in the loop to be prompted again *) echo; echo Unrecognized choice: ${ltr4} ;; esac else echo $message > message.txt break fi done # Show email recipients counter=1 for i in "${recipientArr[@]}" do : if [ "$numRecipients" == "1" ] then echo -n "To: $i" echo "" # Only one recipient so counter not needed elif [ "$counter" == "1" -a "$numRecipients" \> "1" ] then echo -n "To: $i" counter=$((counter+1)) elif [ "$counter" \< "$numRecipients" -a "$numRecipients" \> "1" ] then echo -n ", $i" counter=$((counter+1)) elif [ "$counter" == "$numRecipients" -a "$numRecipients" \> "1" ] then echo -n ", and $i" echo "" counter=$((counter+1)) fi done # Show email sender echo "From: $senderEmail" echo "" # Show email subject echo "Subject: $subject" echo "" # Show email message echo "Message: $message" echo "" # Show email attachments counter=1 for i in "${attachmentArr[@]}" do : if [ "$numAttachments" == "1" ] then echo -n "Attachments: $i" echo "" # Only one attachment so counter not needed elif [ "$counter" == "1" -a "$numAttachments" \> "1" ] then echo -n "Attachments: $i" counter=$((counter+1)) elif [ "$counter" \< "$numAttachments" -a "$numAttachments" \> "1" ] then echo -n ", $i" counter=$((counter+1)) elif [ "$counter" == "$numAttachments" -a "$numAttachments" \> "1" ] then echo -n ", and $i" echo "" counter=$((counter+1)) fi done # End add an attachment yes choice break;; [Nn]) while true do echo -n 'Enter your message: ' read message if [ "$message" == "" ] then echo -n 'Are you sure you do not want to enter a message? (y|n) ' # Retrieve user's response and save as ltr4 read ltr4 rest case ${ltr4} in [Yy]) echo " " > message.txt break;; # Proceed to next step [Nn]) ;; # Stay in the loop to be prompted again *) echo; echo Unrecognized choice: ${ltr4} ;; esac else echo $message > message.txt break fi done # Show email recipients counter=1 for i in "${recipientArr[@]}" do : if [ "$numRecipients" == "1" ] then echo -n "To: $i" echo "" # Only one recipient so counter not needed elif [ "$counter" == "1" -a "$numRecipients" \> "1" ] then echo -n "To: $i" counter=$((counter+1)) elif [ "$counter" \< "$numRecipients" -a "$numRecipients" \> "1" ] then echo -n ", $i" counter=$((counter+1)) elif [ "$counter" == "$numRecipients" -a "$numRecipients" \> "1" ] then echo -n ", and $i" echo "" counter=$((counter+1)) fi done # Show email sender echo "From: $senderEmail" echo "" # Show email subject echo "Subject: $subject" echo "" # Show email message echo "Message: $message" echo "" # End add an attachment no choice break;; *) echo; echo Unrecognized choice: ${ltr2} ;; esac done recipientStr="" recipientStr=$(printf " %s" "${recipientArr[@]}") recipientStr=${recipientStr:1} echo $recipientStr # Add sender email to mail string mailstr="mail -c '"$senderEmail"' " # Add subject to mail string mailstr=$mailstr" -s '"$subject"' " # Add attachments to mail string for i in "${attachmentArr[@]}" do : attachmenttmp=" -a '"$directory"/"$i"' " mailstr=$mailstr$attachmenttmp done # Add recipient emails to mail string for i in "${recipientArr[@]}" do : recipienttmp=" '"$i"' " mailstr=$mailstr$recipienttmp done # Add message.txt to mail string cronlocation=$(pwd) mailstr=$mailstr' < '$cronlocation'/message.txt' echo -n "1) Add a crontab entry to send the email 2) Send the email now " # Retrieve user's response and save as ltr5 read ltr5 rest case ${ltr5} in [1]) while true do echo -n "1) Specify date and time to send email (24 hour clock) 2) Specify time interval to send email (minutes) " read ltr7 case ${ltr7} in [1]) while true do echo -n 'Specify the month (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) ' # Retrieve month from input read month # Check if specified month is a valid choice if [[ $month == "jan" ]] || [[ $month == "feb" ]] || [[ $month == "mar" ]] || [[ $month == "apr" ]] || [[ $month == "may" ]] || [[ $month == "jun" ]] || [[ $month == "jul" ]] || [[ $month == "aug" ]] || [[ $month == "sep" ]] || [[ $month == "oct" ]] || [[ $month == "nov" ]] || [[ $month == "dec" ]] || [[ $month == "JAN" ]] || [[ $month == "FEB" ]] || [[ $month == "MAR" ]] || [[ $month == "APR" ]] || [[ $month == "MAY" ]] || [[ $month == "JUN" ]] || [[ $month == "JUL" ]] || [[ $month == "AUG" ]] || [[ $month == "SEP" ]] || [[ $month == "OCT" ]] || [[ $month == "NOV" ]] || [[ $month == "DEC" ]]; then break else echo "Invalid month specified: $month" fi done while true do echo -n 'Specify the day ' # Retrieve day from input read day # Jan 31 | Feb 29 Check for leap years | Mar 31 | Apr 30 | May 31 | Jun 30 | Jul 31 | Aug 31 | Sep 30 | Oct 31 | Nov 30 | Dec 31 # Check if specified day is within range if [ "$month" == "jan" ] || [ "$month" == "JAN" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in January" fi fi if [ "$month" == "feb" ] || [ "$month" == "FEB" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "30" ]; then break else echo "There are only 29 days in February except on a leap year" fi fi if [ "$month" == "mar" ] || [ "$month" == "MAR" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in March" fi fi if [ "$month" == "apr" ] || [ "$month" == "APR" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "31" ]; then break else echo "There are only 30 days in April" fi fi if [ "$month" == "may" ] || [ "$month" == "MAY" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in May" fi fi if [ "$month" == "jun" ] || [ "$month" == "JUN" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "31" ]; then break else echo "There are only 30 days in June" fi fi if [ "$month" == "jul" ] || [ "$month" == "JUL" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in July" fi fi if [ "$month" == "aug" ] || [ "$month" == "AUG" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in August" fi fi if [ "$month" == "sep" ] || [ "$month" == "SEP" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "31" ]; then break else echo "There are only 30 days in September" fi fi if [ "$month" == "oct" ] || [ "$month" == "OCT" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in October" fi fi if [ "$month" == "nov" ] || [ "$month" == "NOV" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "31" ]; then break else echo "There are only 30 days in November" fi fi if [ "$month" == "dec" ] || [ "$month" == "Dec" ]; then if [ "$day" -gt "0" ] && [ "$day" -lt "32" ]; then break else echo "There are only 31 days in December" fi fi done while true do echo -n 'Specify the hour (24 hour clock) ' # Retrieve hour from input read hour # Check if specified hour is between 0 and 23 inclusive if [ "$hour" -gt "-1" ] && [ "$hour" -lt "24" ]; then break else echo "Hour must be between 0 and 23 inclusive" fi done while true do echo -n 'Specify the minute ' # Retrieve minute from input read minute # Check if specified minute is between 0 and 59 inclusive if [ "$minute" -gt "-1" ] && [ "$minute" -lt "60" ]; then break else echo "Minute must be between 0 and 59 inclusive" fi done echo "$minute $hour $day $month * $year" cronlocation=$(pwd) crondate=$(date | awk '{print $2" "$3", "$6" @ "$4}') crontab -l > "cronbackup $crondate" echo -n 'A backup copy of cron has been saved at '$cronlocation'/cronbackup '$crondate echo "$minute $hour $day $month * $mailstr" >> mycron crontab mycron rm mycron break;; [2]) while true do echo -n "Specify time interval (minutes) " read minute if ! [[ "$minute" =~ ^[0-9]+$ ]] ; then exec >&2; echo "Minute needs to be a whole number" else break fi done hour=$(expr $minute / 60) minute=$(expr $minute - $(expr $hour \* 60)) if [ "$hour" != "0" ]; then hour='*/'$hour else hour='*' fi if [ "$minute" != "0" ]; then minute='*/'$minute else minute='*' fi echo "Hours: $hour" echo "Minutes: $minute" cronlocation=$(pwd) crondate=$(date | awk '{print $2" "$3", "$6" @ "$4}') crontab -l > "cronbackup $crondate" echo -n 'A backup copy of cron has been saved at '$cronlocation'/cronbackup '$crondate echo "$minute $hour * * * $mailstr" >> mycron crontab mycron rm mycron break;; *) echo; echo Unrecognized choice: ${ltr7} ;; esac done ;; [2]) # Execute mail string eval $mailstr ;; *) echo; echo Unrecognized choice: ${ltr5} ;; esac #done