简体   繁体   English

BASH:发送 HTML email 和 boby 中的文件列表

[英]BASH: Send HTML email with a list of files in boby

Trying to use the "find" command to list of files and send as a list instead of a one line list.尝试使用“查找”命令列出文件并作为列表而不是单行列表发送。

expecting outcome on body of the email listing the files (see bodyHTML line as well):期待 email 正文的结果列出文件(也参见 bodyHTML 行):

file1
file2
file3

current outcome:目前的结果:

"file1,file2,file3" <--not want I want on the body of the email.
FOLDER="/temp"
FILES="$(sudo find $FOLDER/*.txt -cmin -60 -type f)"
FILES_COUNT=$($FILES | wc -l)


    EMAIL_TO="###"
    FROM_EMAIL="###"
    FROM_NAME="TEST"
    SUBJECT="TEST"
    TODATE=$(date "+ %D %T")
    
    bodyHTML="<div><H2 style='color:grey;'>$FILES_COUNT Total new files on $TODATE</H2><Table border=1 cellpadding=5 cellspacing=0><TR bgcolor=white align=left><TD><B>$FILES</B></TD></TR>"

    maildata='{"personalizations": [{"to": [{"email": "'${EMAIL_TO}'"}]}],"from": {"email": "'${FROM_EMAIL}'", 
        "name": "'${FROM_NAME}'"},"subject": "'${SUBJECT}'","content": [{"type": "text/html", "value": "'$bodyHTML'"}]}'

    curl --request POST \
    --url https://api.sendgrid.com/v3/mail/send \
    --header 'Authorization: Bearer '$SENDGRID_API_KEY \
    --header 'Content-Type: application/json' \
    --data "'$maildata'"

When I use:当我使用:

FILES="$(sudo find $FOLDER/*.txt -cmin -60 -type f)"

echo $FILES

outcome: 
/temp/test.txt /temp/tes2.txt /temp/test3.txt /temp/test4.txt /temp/test5.txt

When I don't use echo before $FILES:当我在 $FILES 之前不使用 echo 时:

$FILES
outcome: 
Endless loop of the follow below:

"/temp/test1.txt: line 88: +oAtMghU1YTaI+6doyj1Z7cALL5ATHPmrxLQweNYFv2S7WuIryTq9k+LaiQnDo9/: No such file or directory"

I honestly don't unterstand your result.老实说,我不明白你的结果。 But if you want to put the list auf found files into the variable FILES you have to execute the find and assign it's output like you allready do with the FILES_COUNT .但是,如果要将列表 auf 找到的文件放入变量FILES中,则必须执行 find 并将其分配为 output ,就像您对FILES_COUNT所做的那样。 And not assign the command as string.并且不将命令分配为字符串。

So changing this:所以改变这个:

FILES="sudo find $FOLDER/*.txt -cmin -60 -type f"

to this对此

FILES=$(sudo find $FOLDER/*.txt -cmin -60 -type f)

might do the trick.可能会成功。

At least this simplified example returns the desired output:至少这个简化的示例返回所需的 output:

ls -1
file1
file2
file3
FILES=$(find * -type f)
echo $FILES
file1 file2 file3

Did you edit?你编辑了吗?
FILES="sudo find $FOLDER/*.txt -cmin -60 -type f" is just a string... FILES="sudo find $FOLDER/*.txt -cmin -60 -type f"只是一个字符串......
Did you mean FILES=$(sudo find $FOLDER/*.txt -cmin -60 -type f) ?您的意思是FILES=$(sudo find $FOLDER/*.txt -cmin -60 -type f)吗?

The simple answer is that your problem is here: <B>${FILES[*]}</B>简单的答案是您的问题在这里: <B>${FILES[*]}</B>
You are sending the list of files in HTML, which cares not one bit about the embedded newlines.您正在发送 HTML 中的文件列表,它一点也不关心嵌入的换行符。 Your <B>...</B> tags appear only once, at the beginning and end of the list. <B>...</B>标签只出现一次,在列表的开头和结尾。

Assuming your filenames are perfect and pristine with no embedded spaces or anything that will cause this logic to crash and burn, then for a quick and dirty solution, try this -假设您的文件名是完美且原始的,没有嵌入空格或任何会导致此逻辑崩溃和烧毁的东西,那么对于一个快速而肮脏的解决方案,试试这个 -

sed -E 's/^(.*)$/<b>\1<\/b>/g' <<< "$FILES"

You should probably just be assigning with你可能应该只是分配

# DEFINITELY keep the quotes *around* the subshell...
FILES="$(sudo find /temp/*.txt -cmin -60 -type f | sed -E 's/^(.*)$/<b>\1<\/b>/g')"

then instead of <B>${FILES[*]}</B> , just use $FILES .然后代替<B>${FILES[*]}</B> ,只需使用$FILES

You aren't using an array, btw.顺便说一句,您没有使用数组。 ${FILES[*]} isn't buying you anything here. ${FILES[*]}不会在这里给你买任何东西。
FILES is just one big string with all the filenames in it, terminated individually by newlines as returned from the find . FILES只是一个包含所有文件名的大字符串,由find返回的换行符单独终止。

$: x="1
> 2
> 3"
$: echo $x # unquoted strips the formatting (the embedded newlines)
1 2 3
$: echo "$x"
1
2
3
$: echo "${x[*]}" # this is almost never what you want
1
2
3
$: echo "[${x[0]}] [${x[1]}] [${x[2]}]"
[1
2
3] [] []
$: echo "[${x:2:2}]" # if you want substrings, use this (var:start:length)
[2
]

# what we did above:
$: sed -E 's/^(.*)$/<b>\1<\/b>/g' <<< "$x"
<b>1</b>
<b>2</b>
<b>3</b>

Do look carefully at the sections on find in https://mywiki.wooledge.org/ParsingLs though.请仔细查看https://mywiki.wooledge.org/ParsingLs中有关find的部分。 I'd probably make the list with a glob and then carefully check the properly quoted filenames with stat , personally.我可能会用一个 glob 列出列表,然后亲自用stat仔细检查正确引用的文件名。

Good luck.祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM