Automating interactive input
Automating interactive input for command-line utilities are extremely useful for writing automation tools or testing tools. There will be many situations when we deal with commands that read input interactively. An example of executing a command and supplying the interactive input is as follows:
$ command Enter a number: 1 Enter name : hello You have entered 1,hello
Getting ready
Creating utilities that can automate the acceptance of input are useful to supply input to local commands, as well as for remote applications. Let us see how to automate them.
How to do it...
Think about the sequence of an interactive input. From the previous code, we can formulate the steps of the sequence as follows:
1[Return]hello[Return]
Converting the preceding steps 1
, Return
, hello
, and Return
by observing the characters that are actually typed in the keyboard, we can formulate the following string:
"1\nhello\n"
The \n
character is sent when we press return. By appending the return...