tags:

views:

26

answers:

3

I am trying to pass in a string containing a newline to a PHP script via BASH.

#!/bin/bash

REPOS="$1"
REV="$2"

message=$(svnlook log $REPOS -r $REV)
changed=$(svnlook changed $REPOS -r $REV)

/usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php <<< "${message}\n${changed}"

When I do this, I see the literal "\n" rather than the escaped newline:

blah blah issue 0000002.\nU app/controllers/application_controller.rb

Any ideas how to translate '\n' to a literal newline?

By the way: what does <<< do in bash? I know < passes in a file...

+3  A: 

try

echo -e "${message}\n${changed}" | /usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php 

where -e enables interpretation of backslash escapes (according to man echo)

Note that this will also interpret backslash escapes which you potentially have in ${message} and in ${changed}.


From the bash manual: Here Strings

A variant of here documents, the format is:

<<<word

The word is expanded and supplied to the command on its standard input.

So I'd say

the_cmd <<< word

is equivalent to

echo word | the_cmd
Andre Holzner
Nice! It's actually -e rather than -a, but that's exactly what I was looking for.
Chad Johnson
yes, you're right, I was thinking -e but typing -a... I'll correct this.
Andre Holzner
+2  A: 
newline=$'\n'
... <<< "${message}${newline}${changed}"

The <<< is called a "here string". It's a one line version of the "here doc" that doesn't require a delimiter such as "EOF". This is a here document version:

... <<EOF
${message}${newline}${changed}
EOF
Dennis Williamson
+1  A: 

in order to avoid interpretation of potential escape sequences in ${message} and ${changed}, try concatenating the strings in a subshell (a newline is appended after each echo unless you specify the -n option):

( echo "${message}" ; echo "${changed}" ) | /usr/bin/php -q /home/chad/www/mantis.localhost/scripts/checkin.php 

The parentheses execute the commands in a subshell (if no parentheses were given, only the output of the second echo would be piped into your php program).

Andre Holzner
"In order to avoid..." - that would be done if you used `echo -e`
Dennis Williamson