I've been having to write more than my usual share of shell scripts lately that orchestrate a number of other command line utilities and other shell scripts. Some of the scripts I've had to run from my script behaved well but some required user input and did not give any options to provide that input as command line arguments. Since I was not able to modify the scripts that I was calling, I had to find a different way to invoke them.
The scripts that I was calling used the 'read'
command to prompt the user for input like so:
These were interactive scripts but with a predictable pattern for input. Luck was on my side and I knew what each of these inputs should be while my script was being run. All I had to do was provide this input so that the external script would run as if it were a non-interactive script.
There are a number of ways to do the above, I went for something really simple, using echo
and piping its output to the external script, like so:
Note that the echo
string is split across multiple lines, this is completely OK to do. Also note that after my 3rd input value there is a new line, that is because I needed that final return character since the 'read'
command in the external script would be expecting that.
The same approach worked when the external script prompted for a password using 'read -s'
The above approach worked really well in my case, but it's not suitable for every occasion. In more complex situations the 'expect'
command would do a better job, but I'll leave that for another article.
Hope you found this post useful...
...so please read on! I love writing articles that provide beneficial information,
tips and examples to my readers. All information on my blog is provided free of
charge and I encourage you to share it as you wish. There is a small favour I ask in return however -
engage in comments below, provide feedback, and if you see mistakes let me know.
If you want to show additional support and help me pay for web hosting and
domain name registration,
donations, no matter how small, are always welcome!
Use of any information contained in this blog post/article is subject to this disclaimer
Other posts you may like...