我在下面提到的路径中有一个大于10M大小的文件。我需要写一个shell脚本发送电子邮件作为附件,只有当文件大小小于5M。如果文件大小小于10M,我只需要在邮件中突出显示文件的路径。谁能指导一下如何做到这一点?
我尝试了下面的脚本,但它给出-消息文件太长警告。
#!/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
您需要清楚地分解场景你需要处理的,然后为它们编码。您需要区分4种文件状态.
清晰地定义参数有助于可维护性对于每一个状态,分别.
"的位置-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