簡體   English   中英

在bash中格式化電子郵件輸出

[英]formatting email output in bash

我正在編寫一個腳本,該腳本在用戶的AWS訪問密鑰過舊時向其發送電子郵件。

現在,輸出如下所示:

Hello tdunphy, \n Your access key: AKIAICDOHVTMEAB6RM5Q was created on Wed Feb  7 22:55:51 EST 2018 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days. \n Regards, \n Cloud Ops

我希望消息看起來像這樣:

Hello tdunphy,

 Your access key: AKIAICDOHVTMEAB6RM5Q was created on Wed Feb  7 22:55:51 EST 2018 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days.

Regards,
Cloud Ops

這是設置消息正文的行:

 MAIL_TXT1="Hello $user_name, \\n Your access key: $user_access_key1 was created on $date1 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days. \\n Regards, \\n Cloud Ops"

為什么在此示例中換行符不起作用? 我該如何工作?

您可以使用:

template=$'Hello tdunphy, \n Your access key: AKIAICDOHVTMEAB6RM5Q was created on Wed Feb  7 22:55:51 EST 2018 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days. \n Regards, \n Cloud Ops'

然后任一echo起作用:

$ echo "$template"
Hello tdunphy, 
 Your access key: AKIAICDOHVTMEAB6RM5Q was created on Wed Feb  7 22:55:51 EST 2018 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days. 
 Regards, 
 Cloud Ops

printf

$ printf "%s" "$template"
 Your access key: AKIAICDOHVTMEAB6RM5Q was created on Wed Feb  7 22:55:51 EST 2018 and needs to be replaced. All AWS Keys need to be replaced if they are older than 90 days. 
 Regards, 
 Cloud Ops

使用printf您還可以更輕松地組裝模板字段:

$ t2=$'Fields:\n\tf1=%s\n\tf2=%s'
$ printf "$t2" 101 102
Fields:
    f1=101
    f2=102

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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