0

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.

2 Answers 2

0

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).

0

This is just bash escaping issue that you have run into here with Java. Have you tried something like:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.