I have a BASH script that runs various sysadmin functions and system checks. If a check fails, a variable is set to 1
. There are only a couple types of errors that can happen, so I have variables named to describe the error such as $login_error
and $timeout
.
At the end of the script, if an error was encountered, an e-mail is sent to my team. In the e-mail, I include a brief error message describing what the error was. The problem I'm getting is that the error messages contain a \n
character, but my embedded e-mail doesn't preserve the newlines.
I'll show you what I mean. Here's the function that checks for failures and e-mails me:
checkFailure () {
if [[ $error_encountered -eq 1 ]]; then
[[ $timeout -eq 1 ]] && msg="Error 1: The device timed out\n"
[[ $login_error -eq 1 ]] && msg="${msg}Error 2: Failed to login to device\n"
[[ $sky_fall -eq 1 ]] && msg="${msg}Error 3: The sky is falling\n"
{ /usr/bin/nc <ip_of_my_sendmail_box> << EOF
mail from: server@domain.com
rcpt to: me@domain.com
data
to: me@domain.com
subject: Error during systems check
Content-Type: text/plain
$(printf "%b" "$msg")
Sent on: $(date)
.
quit
EOF
} 1>/dev/null
}
Now, every thing here works as intended except that the newlines in $msg
get seemingly stripped from my e-mail. If I wasn't e-mailing this text, then printf will happily interpret the newlines and show me two lines with text.
Let's say that an error 1 and an error 2 happened, I'll get this in my e-mail:
Error 1: The device timed outError 2: Failed to login to device
Why no new line?
If I change the $msg
texts to have two \n\n
characters at the end, like so:
[[ $timeout -eq 1 ]] && msg="Error 1: The device timed out\n\n"
[[ $login_error -eq 1 ]] && msg="${msg}Error 2: Failed to login to device\n\n"
then I get this in the e-mail:
Error 1: The device timed out
Error 2: Failed to login to device
Notice the extra new line between them now.
Does anyone know why I either get no newline or two newlines, but never the desired one newline.