[英]Attaching files using mailx command in bash script
我在以下路径中有 2 个以 .xlsx 扩展名结尾的文件。 一个文件大于 6 MB,另一个文件小于 6 MB。
如果文件小于 6 MB,我需要发送带有文件附件的电子邮件通知。 否则我需要发送一封电子邮件通知,说明该文件大于 6 MB 并且在指定路径中可用..
#!/bin/bash
cd /opt/alb_test/alb/albt1/Source/alb/al/conversion/scr
file= ls *.xlsx -l
#for line in *.xls
min=6
actsize=$(du -m "$file" | cut -f1)
if [ $actsize -gt $min]; then
echo "size is over $min MB and the file is available in specified path -- Need to send this content via email alone"
else
echo "size is under $min MB, sending attachment -- Need to send the attachment"
echo | mailx -a ls *.xlsx -l test@testmail.com
fi
当我运行上面的脚本时,它说 -gt: unary operator expected & ls: No such file or directory
谁能指导如何解决这个问题?
-a
参数只能使用一个文件名,因此您必须为每个要附加的文件重复该参数。 您可以通过遍历所有 xlsx 文件来在数组中构建附件列表,如下所示:
min=6
attachments=()
for file in *.xlsx ; do
[[ -f "${file}" ]] || continue # handles case where no xlsx files exist
if [[ $( du -m "${file}" | cut -f1 ) -le $min ]] ; then
attachments+=( "-a" "${file}" )
fi
done
mailx "${attachments[@]}" -l test@testmail.com
您不需要使用ls
- 这是人类查看其文件系统的工具,脚本不需要它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.