JohnnyLoo JohnnyLoo - 1 year ago 91
Bash Question

Perl argument that contains newline character

I am trying to write a perl script that emails whatever I give it in the first argument, but I need the email body to have multiple lines. When I pass text to the script (Using MIME::Lite, fyi), the email body only has a single line because the "\n" I pass in the script do not seem to be converted to newline in the email body.

Here is an example

Script Brief Content:

my $message = $ARGV[0];
#blah blah
sendEmail $message;

Command Example when calling script "Issue: CPU usage is high \n Time: 11:20AM \n"

What I need:

Issue: CPU issue
Time: 12:00AM

What I get:

Issue: CPU issue \n Time: 12:00AM \n

Even if I just want to print to output that string as in
print "$message"
, I get the same output.

How can I force perl to consider \n characters that are passed to it so that it moves content to next line when it sees that character?

Answer Source

This doesn't look like an issue with perl, it's a problem with the bash command that you typed. When you enter the following string literal in bash:

"Issue: CPU usage is high \n Time: 11:20AM \n"

bash does not interpret the \n character sequences as line breaks. You need a different syntax for that. You can use either of the following:

# bash $'Issue: CPU usage is high\nTime: 11:20AM\n'


# bash or sh 'Issue: CPU usage is high
Time: 11:20AM
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download