shell 使用Bash脚本将文件作为附件发送

8tntrjer  于 2022-12-19  发布在  Shell
关注(0)|答案(1)|浏览(131)

我在下面提到的路径中有一个文件大小超过10 M。我需要编写一个shell脚本,只有当文件大小小于5 M时才能将电子邮件作为附件发送。如果文件大小小于10 M,我只需要突出显示电子邮件中文件的路径。有人能指导如何实现这一点吗?
我尝试了下面的脚本,但它是给-消息文件太长警告.

#!/bin/bash
cd /opt/alb_test/alb/albt1/Source/alb/al/conversion/scripts
#Sending Email

ls *.xlsx -1 > test.txt
while read line
do
mailx -s "Test Email" -a ${line} -s "Attaching files less than 5 MB" abc@testmail.com,def@gmail.com << EOM

Hi, Sending the Files
EOM
done<test.txt
a8jjtwal

a8jjtwal1#

你需要清楚地分解你需要处理的场景,然后为它们编码。你需要区分4个文件状态
当您为这些状态中的每一个明确地定义参数时,分别地,这有助于可维护性。
ls语句中放置“-1”是一个错误。另外,要强制每行一个,不需要指定它,这是定向到文件时的默认值。只有输出到终端时才需要“-1”。
下面是针对您的问题提出的解决方案:

#!/bin/bash

filepath="/opt/alb_test/alb/albt1/Source/alb/al/conversion/scripts"

distro_list='abc@testmail.com,def@gmail.com'

THRESH_file=5000000
THRESH_path=10000000

SIGNATURE="
Me
MyPosition
MyEmail"

cd ${filepath}

#Sending Email

ls *.xlsx > test.txt

while read line
do
    if [ -s "${line}" ]
    then
        SIZE=$(stat --printf="%s" "${line}" )

        if [ ${SIZE} -lt ${THRESH_file} ]
        then
            SUBJECT="-s 'Attaching files under 5 MB'"
            MSG="Hi, Sending the undersized file."
            ATTACH="-a '${filepath}/${line}'"
        else
            if [ ${SIZE} -lt ${THRESH_path} ]
            then
                SUBJECT="-s 'File size at least 5 MB but less than 10 MB'"
                MSG="Hi, Sending the midsized file path:  '${line}' [${filepath}] "
                ATTACH=""
            else
                SUBJECT="-s 'File size at least 10 MB'"
                MSG="Hi, Sending the normal sized file."
                ATTACH="-a '${filepath}/${line}'"
            fi
        fi
    else
        SUBJECT="-s 'File is empty!'"
        MSG="File '${line}' is empty [${filepath}] ..."
        ATTACH=""
    fi

    echo -e "${MSG}\n${SIGNATURE}" |
        mailx ${SUBJECT} ${ATTACH} ${ditro_list}
done <test.txt

相关问题