I have a console program ( a DOS program) that requires interactive input. After typing in the command line, for example
commandline.exe /ShowReport
The DOS prompt will prompt user to key in some values, and then proceed to the next interactive input.
For example, when I typed in the above command, the console will prompt me with the following options:
press '1' to show Report A
press '2' to Show Report B
And I would press '1' if I want to show report A.
The issue now is I want to automate all these things by presetting all the input values in a script files. Maybe something like this ( I don't know)
commandline.exe /ShowReport <1<'abc'
I want to write a batch script for this. Is there any tools that allow me to do that?
You could write all your inputs in a file (say 'input.txt') and use redirection to feed your program with these inputs:
commandline.exe /ShowReport < input.txt
The '<' tells the command prompt to use the content of the file on the right hand side as standard input.
Not strictly a batch solution but this might do the trick:
Expect
See also the Wikipedia entry.
Related
I use Spyder as my Python IDE. In most of what I do I use the editor to write code and execute portions of the program one at a time. I typically go back and forth, change things, add pieces, execute portions... you get it. Is there a way to save to file what Spyder shows in the console portion of the IDE after each portion of code is executed?
(Spyder dev here) Well, there is a way to save the output shown in our consoles but not after each execution. You have to do it manually, like this:
For our Python consoles, you need to do a right mouse click over any console and select the option Save history log.
For our IPython consoles, you need to do a right mouse click over any console and select the option Save as HTML/XML
One way to capture entire console output of Sypder IDE:
1. Go to "Profile" (Shortcut: F10) under "Run" Tab.
2. On Right hand side, you can see Run, Stop and Output buttons.
3. Click on Run and the entire console session is captured under Output.
Hope, this helps. Kindly correct me if I am wrong.
I have an expect script in which I am currently looking for multiple prompt types and sending commands in response. I am aware of regular expression matching using "-re" but I'd like to know of the correct way to achieve this.
For example, I have these prompt types:
[user#hostname ~]#
user#hostname --->
/ >
-bash-3.00$
cli>
Is this the correct/sufficient expression to detect all the above?
set multiPrompt "(%|#|cli\>|\$|\-\-\-\>)"
expect -re $multiPrompt
send "$someCommand\r"
Also, I have a list of commands, some of them cause the prompt to change after they are executed on the remote system. Due to the change in prompt, the remaining commands are not getting sent because my expect script is unable to detect the change and perform the send action.
What I'm trying to do is create a pool of possible prompts, so that my expect script sends the commands across without missing any of them. Is my approach correct?
While using a regular expression to detect the prompts is the right thing, choosing a good one is tricky when you've got such a wide range of possibilities. For example, I bet that this RE would work:
set multiPrompt {[#>$] }
(It just detects the end of the prompt, and ignores all the stuff before it. There's virtually always a space at the end of the prompt, used to visually separate what users type from the prompt.)
However, the problem is that this RE is fairly likely to match other things. You might instead be better off changing the prompt to a known-and-unique value (typically by setting the PS1 environment variable on the remote device) so that you get reliable detection. Mind you, that's only suitable when you're not exposing the prompts back to users, which is true for some uses of expect and not others…
I know it's an old thread, but for anyone searching on this I hope this helps. I am a UNIX sysadmin and have a handful of expect scripts my program calls on for various admin functions. This is the one solution I found that works in all my use cases:
set prompt "(%|#|>|\\$ )"
set prompt [string trim $prompt]
The [string trim $prompt] handles the scenarios where some prompts have a space before the input and some don't by trimming off the space when it looks at the prompt. Example: "password:" vs. "password: "
I'm trying to write a simple GUI for Wget. I'm looking for advice on how to read information from the command line output that Wget generates when it is doing a run. I'd like to update that download information real time to a list box or some equivalent. The GUI will be in Visual Basic. I know programs like WinWget do this, and their source code is available, but I don't know the language that's written in well enough to find what I'm looking for.
tl;dr: I need to update a list box real time with command line output.
There are two ways to use the output of one console application for the input of an other:
The first way is to use the | operator; for example:
dir |more
The second way is to write the data into a file and process it later.
dir > data.txt
I have a test program which prompts for input from user(stdin), and depending on inputs, it asks for other inputs, which also need to be entered.
is there a way I can have a script do all this work ?
There's a program called expect that does pretty much exactly what you want -- you can script inputs and expected outputs and responses based on those outputs, as simple or complex as you need. See also the wikipedia entry for expect
I may have misunderstood, but do you have a program which reads the input and does something with it, and you just want to know how to automate providing it some test input?
For a given test case, does the input you provide have to depend on the output from the program, or is it the same every time?
If the input for a given test is the same every time, then just put it in a text file and redirect stdin for your program to read from that file:
myprogram.exe < input.txt
If the input is different each time, for the same test, then this doesn't help. But for a typical simple test, you just want to answer "y" to the first question, "n" to the second, and "hello world" to the third, or whatever.
In the general case, yes, there is.
For more specific tasks, you can get other tools to do the job that will be more specialized and usable for that particular task.
I've just completed the coding section of simple homework assignment for my C++ class. The second part of the assignment requires us to verify our code's input validation. (The program takes several different values as inputs from a user and prints those values to a file)
I was hoping that I could use bash script for this. Is there any way to use bash script to run and interact with a program? How can I put the program's output into a variable (note that program has a series of input requests and outputs).
Thanks
To build on #Travis' answer, create two files: one holds your inputs (input.txt) and one holds the expected output (expected_output.txt). Then do the following:
./myprogram <input.txt >output.txt
diff output.txt expected_output.txt
If the diff command has any output, there's a problem.
You can do much of this with a shell script but you might want to consider using some other testing tools instead like CppUnit or expect.