Please note, I did not write any of these script, Java code, or the application and I am restricted as to what I can modify in these scripts.

I have a Bash script that starts a Java console /foo/bar/myscript.sh. Specified inside, it calls to start the Java console but prompts for a password before the shell is launched. I am trying to pass a command into the Java console that executes a backup procedure for an application.

Is there a way to run the script which passes the password? The password is not set via Bash but somewhere in Java and I don't know the variable name.

Is there also a way to pass the Java method in the same line? This is the normal, manual process:

  1. ./myScript.sh
  2. Please enter password: myPassword
  3. javaConsole % backup.method("/backup/dir");

Obviously this isn't correct but ideally, a command would go like this:
echo myPassword | ./myscript.sh | backup.method("/backup/dir");


Expect, probably, as this can spawn your script, answer the password prompt, and then I'm not sure what your Java console prompt looks like or what exactly it expects, but:

#!/usr/bin/env expect
spawn -noecho ./myScript.sh
expect -ex password
send -raw "Hunter2\r"
expect -ex javaConsole
send -raw "backup.method(\"/backup/dir\");\r"
# and either you'll need to wait or interact with the java
# to keep it running...

You could also run a trial session with autoexpect and then see what that generates for automation.

echo 'myPassword
/backup/dir' | /foo/bar/myscript.sh
  • The problem with this is it needs to be invoked in a single command strung together. The above solution seems to work. The application I'm launching is quirky. – Greg Jan 10 '18 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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