Search code examples
javabashcentos6rackspacejclouds

jclouds ScriptBuilder not sending the statement correctly to execute


I am trying to build a bash script dynamically and created the file on rackspace centos instance and one of the following behave differently not sure what i am missing here.

String script = new ScriptBuilder()
.addStatement(exec("echo ' expect \"Enter Password :\"'  >> config.sh "))    
.addStatement(exec("echo ' send -- \"123\"'  >> config.sh "))               
.addStatement(exec("echo ' send -- \"\\r\"'  >> config.sh "))   
                .render(OsFamily.UNIX);             

When I loggin to the box and view config.sh .

expect "Enter Password:"
send -- "123"
"send --"

But I want to see

expect "Enter Password:"
send -- "123"
send -- "\r"

I am not sure what I am doing wrong.


Solution

  • Instead of directly using the ScriptBuilder class, it is a better idea to use the Statements helper class. It has a couple methods that might help you:

    • Statements.createOrOverwriteFile: I'd use this. You could have the script pre-created in a file in the class path, and just read the contents of the file to create the equivalent one in the server.
    • Statements.appendFile: You can use this statement to append a set of lines to a file.

    Both scripts are a better option that directly using the ScriptBuilder. If you end up with more than one statement, you can add them to a StatementList (which is also a Statement).