I'm trying to automate a process that creates a binary response file using a java program wrapped by a sh script (so many things wrong with this, but not my code). The result I want requires hitting [enter] for the first prompt and 'Y'[enter] for the second prompt when running the sh script.

I got it to work on one Linux box using the following code:

echo Y > ans; ./script < ans `echo -e \\\r`

On another Linux box, it's not working (script goes into an infinite loop, again, not my code). Both boxes run the same kernel and version.

There are more elegant, straightforward ways of doing this, I'm sure. However, I need it to work consistently. I'm not convinced it isn't the way one of the boxes is setup.

  • 2
    It would probably be more reliable to run expect for this kind of thing. – Tom Hunt Oct 26 '15 at 17:06
  • (echo; echo Y) | ./script ? – Jeff Schaller Oct 26 '15 at 17:30
  • Hi! Do you know which part of your code is looping? Do you know for sure it's looping (as opposed to just hanging waiting for more input)? Also, I'm not sure exactly what input you want the code to get; if it's the characters "Y\r\n", then printf "Y\r\n" | ./script should do it. – dhag Oct 26 '15 at 17:30
  • 1
    The command ./script < ans `echo -e \\\r`⁠ doesn't make any sense. It invokes ./script with an argument that is a carriage return, and with input from the file ans — i.e., it's like ./script $'\r' < ans. I'm going to make a wild guess that the argument is ignored. Then I'm going to make an educated guess that the program issues the first prompt, reads the Y line from the ans file, issues the second prompt, and tries to read a second line of input. But, since it's at the end of the ans file, it gets an EOF. Some (stupid) programs react to EOF by trying the read again, forever. – G-Man Oct 26 '15 at 18:06

Try a simple expect script:


set timeout 20
spawn /path/to/your/java/shell/script/thing.sh

expect "FirstPrompt:" { send "\r" }
expect "SecondPrompt:" { send "Y" }

...where FirstPrompt is the text of the first prompt (so if your shell script thing promps "Please press ENTER" then you need that, verbatim, where I have written "FirstPrompt:" in the sample expect script) and SecondPrompt is the second promt, verbatim.

...I also assume expect is installed, and is located at /usr/bin/expect on your system. If that is not the case, then install it, and use the correct path.

  • Installed inspect and wrote a script. For some reason the second prompt refuses to acknowledge the send. Just stops; waiting for a human response. – Stephen Pliska-Matyshak Oct 26 '15 at 19:45
  • #!/usr/bin/expect spawn "/u01/app/" expect "Email address/User Name: " { send "\r" } expect "Do you wish to remain uninformed of security issues (\[Y\]es\, \[N\]o) \[N\]: " { send "Y" } interact – Stephen Pliska-Matyshak Oct 26 '15 at 19:52
  • try 'Y\r' as the response. – Klaatu von Schlacker Oct 26 '15 at 19:58
  • same result; just hangs waiting for human intervention – Stephen Pliska-Matyshak Oct 26 '15 at 19:58
  • 1
    It has to be something about this script which is supplied by Oracle. I've been encountering a number of frustrating quirks making it nearly impossible to automate a number of processes regarding the installation and patching for Oracle software. It's like do it manually or use Cloud Control, but heaven forbid let this work using Chef or any other useful OS tools. – Stephen Pliska-Matyshak Oct 28 '15 at 13:25

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.