I am creating a bash script for generating certificates. The openssl command that creates a certificate asks for keyboard input. This is every time the same sequence of keys: seven times [ENTER] followed by two times ['y' + ENTER]. How can I do this programmatically?
Since it's asking for keyboard input, just give it something to read.
I suppose yes | your_script
would work, otherwise you could just write the following sequence to its input:
\n\n\n\n\n\n\ny\ny\n
One way to simulate user interaction is expect.
With OpenSSL specifically, you could just write a configuration file that does not require any input for the task you want to perform. (see man 5ssl config
)
Expect is pretty good at this kind of thing. Other languages will let you do this (far less conveniently) through their popen-style facilities.
You might be able to pipe the characters in as @tusbar suggests, but tools like openssl may insist on you typing it in (something Expect gets around by setting up pseudo-terminals).
You can also use a here document to include input directly in your bash script:
interactive-program <<LimitString
command #1
command #2
...
LimitString
(But the configuration file option suggested by @hop is still the best idea).