我正在使用 ksh。
我需要从文件中读取数据到变量中,然后进一步使用它们来发送电子邮件。
- 文件可以由任何较少使用的字符(如 | ^ 等)或字符组分隔。
- 需要找回邮件发件人、邮件收件人、抄送、密件抄送、主题、正文从文件。
- 文件只有一条记录。
- 我从表中假脱机到文件中,因此分隔符可以是任何字符,但在一般英语中较少使用,因为像 , & * 等字符可能出现在正文中,并且可能返回错误的值。
文件:(抄送和密件抄送文件中不可用,即它们是空白的)
[email protected]|[email protected]|||TEST EMAIL FOR LMS ERROR|Hi <<FIRST_NAME>>, <br><br>
Following errors are generated during migration of LMS data into FIMS application.<br><br><br>
The respective details of Error(s) occured is logged into the attached file.
Regards,<br>
FIMS Web Application<br><br><br>
This is an auto-generated e-mail, please don't reply to this e-mail
Reply to the following person for further details:
[email protected]
代码使用:
while IFS='|' read -r a1 a2 a3 a4 a5 a6
do
flag1=`echo $a1`
flag2=`echo $a2`
flag3=`echo $a3`
flag4=`echo $a4`
flag5=`echo $a5`
flag6=`echo $a6`
done < $RUNTIME/EMAIL_$System`date +%y%m%d`.csv
它不是设置变量。
当使用下面的代码时:它显示了不需要的输出:
while IFS='|' read -r a1 a2 a3 a4 a5 a6
do
echo $a1
echo $a2
echo $a3
echo $a4
echo $a5
echo $a6
done < $RUNTIME/EMAIL_$System`date +%y%m%d`.csv
输出:(地狱有很多空行)
[email protected]
[email protected]
TEST EMAIL FOR LMS ERROR
Hi <<FIRST_NAME>>, <br><br>
Following errors are generated during migration of LMS data into FIMS application.<br><br><br>
The respective details of Error(s) occured is logged into the attached file.
Regards,<br>
FIMS Web Application<br><br><br>
This is an auto-generated e-mail, please don't reply to this e-mail
Reply to the following person for further details:
[email protected]
答案1
由于文件第一行中包含所有字段,因此可以使用以下代码:
IFS='|' read -d ^ -a field < "$RUNTIME/EMAIL_$System`date +%y%m%d`.csv"
请注意,^
可以是文本中不应该出现的任何符号,以便将其操作到最后。
所有必需的字段将存储到数组中场地 从 0 元素开始:字段[0],领域[1]...领域[5]并可以通过以下方式检查
unset i
for element in 'mail from' 'mail to' cc bcc subject body
do
echo "$element : ${field[i++]}"
done