How to use expect to execute a jar and continuously hit enter?

672 Views Asked by At

To start from the beginning, I'm using ansible to open a Jar file in a linux environment.

Basically I do: java -jar someJarFile.jar, and it opens in command line. Now, it prompts me with several questions which can be passed by hitting Return multiple times. Finally the jar will produce a WAR file...

Here's where I need your help. I want to use ansible to call an expect script so that it can automatically execute the jar and continuously hit enter until the jar is successfully completed/installed.

2

There are 2 best solutions below

0
On BEST ANSWER

try

yes "" | java -jar someJarFile.jar
0
On

If you know the number of times you need to press <Enter> you can use echo -e '\n'. For example, if you know you'll need three <Enter>s:

echo -e '\n\n\n' | java -jar someJarFile.jar

This is also helpful for times when you need to input specific and distinct values which yes doesn't allow. For example, answering a prompt like:

Enter value [default]: <enter>
Enter number: 30<enter>
Ok?: y<enter>
Do the dangerous: n<enter>

Could be simulated by

echo -e '\n30\ny\nn\n' | java -jar someJarFile.jar