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.

Community
  • 1
  • 1
Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144
  • Syntax of here document: https://en.wikipedia.org/wiki/Here_document#Unix_shells – Cyrus Aug 19 '16 at 12:50
  • remove the space replace by dash (as stated in the link): `sudo su auto_deploy <<-EOF` – Jean-François Fabre Aug 19 '16 at 13:00
  • 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 `...<EOF` ? Even if that fixes it, you may get a new error message. I'm skeptical (but will be happy to learn otherwise) that ` – shellter Aug 19 '16 at 13:08
  • @Jean-FrançoisFabre I did that and got the same error. – Sandeepan Nath Aug 19 '16 at 13:14
  • @shellter I am skeptical about that too. I did not understand what you meant by the `|sudo` there. – Sandeepan Nath Aug 19 '16 at 13:16
  • 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 ` – shellter Aug 19 '16 at 13:21
  • 2
    Possibly relevant: [How to save newlines in XML attribute?](http://stackoverflow.com/questions/2004386/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. – chepner Aug 19 '16 at 13:45
  • @chepner could you please elaborate that? I checked the other question, but am not getting what exactly is written there. – Sandeepan Nath Aug 22 '16 at 06:50
  • 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. – tripleee Aug 22 '16 at 09:35
  • @tripleee 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. – Sandeepan Nath Aug 22 '16 at 11:07
  • Possible duplicate of http://stackoverflow.com/questions/6995019/echo-linebreak-to-file-using-phing-on-windows – tripleee Aug 22 '16 at 11:20
  • @tripleee, As mentioned in that question, I tried to do this, `` but it still throws the same error. Not sure what am I missing. – Sandeepan Nath Aug 22 '16 at 12:09
  • @Sandeepan Have you tried ``? – Tomalak Aug 22 '16 at 17:55
  • @Tomalak, no I didn't try that. You mean the ` ` after the starting `EOF` and without any ending `EOF`? – Sandeepan Nath Aug 23 '16 at 13:07
  • I mean ` ` (the encoded newline character) in every place that you want to have newline characters in your attribute value. Just like you use `<` (the encoded left angle bracket character) in every place where you want to have a `<` in your attibute value. – Tomalak Aug 23 '16 at 13:49

1 Answers1

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
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 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. – Sandeepan Nath Aug 22 '16 at 13:43
  • 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. – Sandeepan Nath Aug 22 '16 at 13:53
  • Right, I was using `-verbose` for the ability to see the generated script in order to better diagnose the problem. – larsks Aug 22 '16 at 14:13