Unable to use here-document delimited by EOF within phing XML
Asked Answered
W

1

0

I have some commands to be run after switching to a different user. I need to do this in a build xml file.

Following is what I have done -

<exec command="sudo su auto_deploy &lt;&lt; EOF
echo 'Logged in user'
whoami
EOF" dir="${dir.scratchpad}" />

I have used XML escaping, i.e. &lt; for <.

However, I am getting the following error -

sh: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')

Related question - here-document gives 'unexpected end of file' error

Update

Note - I have not put any space after the starting EOF and before the ending EOF.

Update 1

Added bounty. Expecting an elaborate answer because I am unable to make much sense from the comments so far. Pardon my lack of knowledge.

Update 2

Just in case it was not clear, I am working on Phing, and the XML that I mentioned above is from the build xml file that Phing allows a user to write, to do deployment related stuff.

Update 3

As mentioned in the question referenced by @tripleee, I tried with this -

  <exec command="sudo su auto_deploy &lt;&lt; EOF${line.separator}echo 'Logged in user'${line.separator}whoami${line.separator}EOF" dir="${dir.scratchpad}" />

but it still throws the same error. Not sure what am I missing.

Walters answered 19/8, 2016 at 12:45 Comment(15)
Syntax of here document: en.wikipedia.org/wiki/Here_document#Unix_shellsTheology
remove the space replace by dash (as stated in the link): sudo su auto_deploy &lt;&lt;-EOFFostoria
I interpret the error message here-document at line 0 as "I didn't get anything for input". Did you try removing the space, so you have ...&lt;EOF ? Even if that fixes it, you may get a new error message. I'm skeptical (but will be happy to learn otherwise) that <exec ... can take a multiline input. Could you echo "echo 'user'; whoami" |sudo ... instead? Good luck.Schaub
@Jean-FrançoisFabre I did that and got the same error.Walters
@Schaub I am skeptical about that too. I did not understand what you meant by the |sudo there.Walters
Here-Docs send data to the reading process's std-in. So does echo "xxx " | command. But with the sudo su in the way, it's likely that won't work. maybe <exec command="sudo su \"echo 'logged in';whoami\" | auto_deploy... ? Good luck.Schaub
Possibly relevant: How to save newlines in XML attribute?. If the XML processor is normalizing the newlines to spaces, then the here document is being interpreted as a list of additional arguments to sudo, and so the terminating EOF is never found.Salome
@Salome could you please elaborate that? I checked the other question, but am not getting what exactly is written there.Walters
What program extracts the command from this exec element? It looks like that's the one which isn't handling newlines correctly. There is nothing in XML syntax specifically to define how exactly this should work; it's up to the tool to decide whether and if so how to process multi-line attributes.Cindacindee
@Cindacindee I am working on Phing, and the XML that I mentioned above is from the build xml file that Phing allows a user to write, to do deployment related stuff.Walters
Possible duplicate of #6995519Cindacindee
@tripleee, As mentioned in that question, I tried to do this, <exec command="sudo su auto_deploy &lt;&lt; EOF${line.separator}echo 'Logged in user'${line.separator}whoami${line.separator}EOF" dir="${dir.scratchpad}" /> but it still throws the same error. Not sure what am I missing.Walters
@Sandeepan Have you tried <exec command="sudo su auto_deploy &lt;&lt; EOF&#xA;echo 'Logged in user etc'" etc="etc." />?Swot
@Tomalak, no I didn't try that. You mean the &#xA; after the starting EOF and without any ending EOF?Walters
I mean &#xA; (the encoded newline character) in every place that you want to have newline characters in your attribute value. Just like you use &lt; (the encoded left angle bracket character) in every place where you want to have a < in your attibute value.Swot
S
2

I've never used phing before, but looking at the documentation it looks like there are a couple of ways to solve your problem. First, using the -verbose option it looks like your original solution might Just Work if you add an additional newline after the final EOF. That is, if I have this build.xml:

<?xml version="1.0"?>
<project name="chamilo" default="clean" basedir=".">
<target name="test">
<exec command="sudo -u deploy bash &lt;&lt;EOF${line.separator}echo Logged in user;whoami${line.separator}EOF" />
</target>
</project>

And I run phing -verbose test, I see:

     [exec] Executing command: sudo -u deploy bash <<EOF
echo Logged in user;whoami
EOF 2>&1
sh: line 2: warning: here-document at line 0 delimited by end-of-file (wanted `EOF')
     [exec] Logged in user
     [exec] deploy
     [exec] bash: line 2: EOF: command not found

Look at the final line of the generated script, which looks like:

EOF 2>&1

If I add an additional newline, like this:

<?xml version="1.0"?>
<project name="chamilo" default="clean" basedir=".">
<target name="test">
<exec command="sudo -u deploy bash &lt;&lt;EOF${line.separator}echo Logged in user;whoami${line.separator}EOF${line.separator}" />
</target>
</project>

Then it Just Works:

     [exec] Executing command: sudo -u deploy bash <<EOF
echo Logged in user;whoami
EOF
 2>&1
     [exec] Logged in user
     [exec] deploy
Schinica answered 22/8, 2016 at 13:23 Comment(3)
Wow, the ending ${line.separator} did the trick. Even the -verbose option was not necessary. You, Sir/Madam, deserve a medal! The bounty is definitely your. Need to wait for 20 hours.Walters
Ohk now I understood why you used the verbose. The command written there works, but for showing some output, like echo output, -verbose is needed.Walters
Right, I was using -verbose for the ability to see the generated script in order to better diagnose the problem.Schinica

© 2022 - 2024 — McMap. All rights reserved.