2012-08-22 28 views
0

我很難獲取這個shell腳本運行。我決定開始在我的腳本中使用簡單的函數,因爲它更具可讀性和可維護性。這是我第一次嘗試,並且一切運行良好,但郵件功能掛起。我知道郵件等待EOF,但我正在使用echo將我的身體輸入到EOF中。掛在郵件函數上的shell腳本

我想我已經做了一些錯誤的引用或部分傳遞參數在shell腳本中,我不完全理解。我嘗試了所有我能想到的引號組合,但它每次都會掛起。

#!/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 - [email protected]" 

     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 

回答

1

您的mail()函數與郵件命令具有相同的名稱。 將mail()重命名爲mymailwrapper()之類的東西,否則它只是遞歸地調用它自己。

+0

謝謝你,我早點解決了這個問題。這是那些facepalm時刻之一。 :) – tpederson