#!/bin/bash
# Email details
RECIPIENT="recipient@example.com"
SENDER="sender@example.com"
EMAIL_SUBJECT="Query Results"
# SQL script files
SQL_FILES=("query1.sql" "query2.sql" "query3.sql")
# Execute the SQL scripts and store results in HTML format
OUTPUT_FILES=()
for ((i = 0; i < ${#SQL_FILES[@]}; i++)); do
SQL_FILE="${SQL_FILES[i]}"
OUTPUT_FILE=$(date +%Y%m%d%H%M%S)_query$i.html
OUTPUT_FILES+=("$OUTPUT_FILE")
# Run the SQL script using SQL*Plus
sqlplus -S app/apps@vision @"$SQL_FILE" > "$OUTPUT_FILE"
done
# Prepare the email body with results of each query
EMAIL_BODY=""
for ((i = 0; i < ${#OUTPUT_FILES[@]}; i++)); do
OUTPUT_FILE="${OUTPUT_FILES[i]}"
if [ -f "$OUTPUT_FILE" ]; then
HTML_BODY=$(cat "$OUTPUT_FILE")
# Append HTML content to the email body
EMAIL_BODY+="\n\nQuery $((i+1)) Results:\n\n$HTML_BODY"
# Remove the temporary HTML file
rm "$OUTPUT_FILE"
fi
done
# Send email using mailx
echo "$EMAIL_BODY" | mailx -s "$EMAIL_SUBJECT" -r "$SENDER" "$RECIPIENT"
echo "Email sent successfully."
No comments:
Post a Comment