Using echo in bash shells called from an ant sshexec task
Facts - Java
Sunday, 06 November 2011 20:18

This note has been tested with ant 1.8, GNU Bash 3.0 and echo 5.2

With the bash command echo one can append lines to a file. Use the -e option to pass special characters such as new lines:

$ echo -e "append\ntwo lines" >> afile.txt

When doing this from the ant sshexec task, within a groovy task, then one has to add an extra backward slash:

def appendTxt = "append\\ntwo new lines"
ant.sshexec (
  trust:"true",
  host:aHost,
  username:properties.get("ssh.user"),
  password:properties.get("ssh.password"),
  command:"echo -e \"" +appendTxt +"\" >> afile",
  failonerror:"false"
) { }

Note that the quotes around the text argument should be escaped as well.

Why did I have to do this? Answer: for a scripted deployment I needed to append some additional lines to a configuration file.