簡體   English   中英

在 bash 腳本中使用 mailx 命令附加文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM