我很难让这个 shell 脚本运行。我决定开始在我的脚本中使用简单的函数,因为它更具可读性和可维护性。这是我的第一次尝试,一切运行良好,但邮件功能挂起。我知道邮件在等待 EOF,但我正在使用 echo 将我的身体放入其中。
我想我在我不完全理解的 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 - $@"
echo $MESSAGE
echo $MESSAGE >>$LOGFILE
}
mail(){
BODY="$1"
SUBJECT="$2"
echo "$BODY" | mail -s "$SUBJECT" my@email.com
}
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