tpederson
tpederson

Reputation: 526

Shell script hanging on mail function

I am having difficulty getting this shell script to run. I decided to start using simple functions in my scripts as it's more readable and maintainable. This is my first attempt, and everything runs great, but the mail function hangs. I know that mail waits for EOF, but I am piping my body into it using echo.

I imagine I've done something wrong with quoting or some part of passing arguments in shell scripts that I don't fully understand. I've tried every combination of quotes I can think of, but it still hangs every time.

#!/bin/bash

if [ -f /tmp/imports ]
then
        echo "Script Off"
        exit
fi

OLDIFS=$IFS
IFS=$'\n'
IFS=$OLDIFS
LOGFILE="/var/log/file.log"

log(){
        DATE=`date "+%m-%d-%Y %H:%M"`
        MESSAGE="$DATE - $@"

        echo $MESSAGE
        echo $MESSAGE >>$LOGFILE
}

mail(){
        BODY="$1"
        SUBJECT="$2"

        echo "$BODY" |  mail -s "$SUBJECT" [email protected]
}

log "Script Started"

TodayFiles=`find /ftpfiles/incoming/ -type f -mmin +60`
Count=`find /ftpfiles/incoming/ -type f -mmin +60 | wc -l`

log "$Count Files Found"

if [ $Count -gt 4 ]; then
        log "Too Many Files!!!\n"
        mail "There were 5 or more files found by the script. I have not moved any files at this time. There is likely something very wrong. Please check things out immediately." "ATTN: TOO MANY FILES!"

fi

if [ $Count -gt 0 ]; then
        log "Scanning Files..."
        for Source in $TodayFiles
        do
                log "Found file: $Source"
                #mv "$Source" "/ftpfiles/rejected$Source"
                log "Moved file to: /ftpfiles/rejected$Source"
                mail "The file $Source has been moved to rejected. Please find out why and send the proper rejection. Thank You." "ATTN: Stranded Import file Found"
        done
fi

Upvotes: 0

Views: 311

Answers (1)

moopet
moopet

Reputation: 6185

Your mail() function has the same name as the mail command. Rename mail() to something like mymailwrapper() otherwise it's just calling itself recursively.

Upvotes: 1

Related Questions