Shell script - input redirection when prompted by the shell script multiple times Shell script - input redirection when prompted by the shell script multiple times unix unix

Shell script - input redirection when prompted by the shell script multiple times


What you can use is the program expect. You create a script for it that tells it when to give what input to some command it executes. This way you can automate exactly the kind of thing you're struggling with.

More info on Google and here:


You say 'it only reads the first line of input.'

So you have to kill the script?

Is there any output? (error messages especially)?

Are you redirecting STDERR to /dev/null or else where? If so, remove that.

Here is the hightest probability helper ... Modify the top-level script and add set -vx at the 2nd line. Then you'll be able to see what was processed, where it has stopped and possibly formulate theories about why it is not processing data.

Any chance that the input file was created in a Windows environment and the cr\lf pair is messing up the expected input?

I hope this helps.


Thanks all for commenting and answering. I tried except and that did not work. So I am going to mention what worked for us. Here was our workflow - 1. At the linux prompt, type the command, it was connect() in our case. 2. Once that command is given, the script would ask for parameters for the command like port number, server etc. we had to provide that manually 3. Then we again are presented with a shell prompt with another input. In our case, we were able to provide the first command connect() at the prompt using file redirection, but the parameter passing was an issue. The solution we found was provide the parameters inside the parentheses of connect only i.e. our input file for redirection would contain - connect(). This worked for us.