How do you enter something at a console prompt programmatically? - batch-file

I have program, that must interact with a console program before my program can continue what it is doing. I'm trying to avoid my user from having to interact with this dos program. So, I created a .bat file that does everything I need to do except for the last step which still requires user interaction that I'm trying to avoid.
Specifically, the command I type ends up at a prompt where I need to automatically enter y and then Enter (to say yes to the prompt) and then I want to exit out.
Is there any way that I can make this happen automatically without my user having to enter y and Enter? Ideally, I'd like to have the console window NOT even pop up while this is going on.

You can pipe in a 'y' character into the program like so:
echo y | executable.exe
Multiple lines can be entered like so:
(echo y
echo n) | executable.exe
...which will pass first 'y' then 'n'.
See tip from Microsoft here.

The post from Microsoft also clearly says :
Do not type a space between the "y" and the pipe symbol (|)
and indeed, I noticed that in my case
echo y | executable.exe
doesn't work
while
echo y| executable.exe
works fine

I used the following, since "echo y | executable.exe" didn't worked for me
// Write a "Y" to the process's input
proc.StandardInput.WriteLine("Y");
// Now that we've sent the confirmation "Y" wait for the process to exit
proc.WaitForExit();
as posted here: https://www.experts-exchange.com/questions/27024185/C-ProcessStart-How-to-automatically-press-the-Y-key.html

Related

Command Line Program with Incorrect Ordering

I am currently have a C program that asks the user for input prompts.
My program has a "Please enter: " as the user prompt.
I am currently trying to automate this with a bash script, but the input
ordering keeps getting messed up.
In my bash script, if I try to do echo "ls" | ./program_executable, I get
Program output
Please enter:
When what I want is:
Please enter: ls
Program output
Are there any other methods I could use to accomplish this?
Thank you.
fflush(stdout) after the printf() should solve the problem.

How to redirect input in Windows CMD without trailing empty inputs?

I have a program that can be executed through the windows console, will prompt the user for number corresponding to an action (I want to input 3, which corresponds to "Send Script"), and will then prompt the user for another number that tells it which script to send. I have attempted creating a file (in.txt) with the contents:
3
3
and sending the command program.exe < in.txt . However, when this runs, after it inputs both of the values and the correct script starts running, it continues to send values to the program creating constant Press 0 for help messages clogging up the output. How can I send just the 2 values I want to send and terminate the input stream after?
A more concrete example of what I am looking for would be way to execute a batch file that consists of
pause
pause
pause
pause
pause
where I call it by redirecting an input file consisting of 2 inputs to it (5pause.bat < input.txt), and the output would be that 2 of the pauses have been passed, but 3 have not.

Logic to determine whether a "prompt" should be printed out

Seems like a basic idea: I want to print out a prompt for a mini shell I am making in C. Here is an example of what I mean for the prompt:
$ ls
The $ being the "prompt". This little mini shell I am making supports backgrounding a process via the normal bash notation of putting a & symbol on the end of a line. Like $ ls &.
My logic currently is that in my main command loop, if the process is not going to be backgrounded then print out the prompt:
if(isBackground == 0)
prompt();
And then in my signal handler I print out the prompt using write() which covers the case of it being a background process.
This works fine if the background command returns right away like with a quick $ ls &, but in the case of something like $ sleep 10 & the shell will look like it is being blocked as the prompt will not be printed out until it hits the signal handler.
I can't figure out how to fix this because I don't know when the background process will end which means that the signal handler somehow needs to be saying when to print the new prompt because if the background process happened to have an output, it would output and then there would no longer be a prompt.
How can I resolve this problem? Is there a better way to do this that I'm not thinking of that could resolve my problem?

Getting keystroke from batch file

I am trying to achieve the following
I have a C program whose basic layout is as below and Press any key to continue is not starting of my program, this statement comes in middle of the program i.e; after taking some input commands then program comes to this point.
Let my program be stack_supporter.c . As an input it will take some options and perform some calculations and then comes to below point.
printf("Press any key to continue");
getchar();
calling_relevant_function();
I am using below batch file:
cd program_location_directory_path
./stack_supporter -o # -o is used to give options
and I am stuck in the middle of the program waiting for key press event. so how to automate these type of programs ?
echo(|myProgram.exe
You can pipe something into the program standard input

Interactive input that doesn't appear on the in the window in C

I'm writing a program that needs to ask the user a yes or no question at the end. Going based off the example .exe my teacher provided us, the line is supposed to print out "Would you like to print an Amortization Table(Y/N)?Y" and it looks for one keystroke from the user. The Y is printed out following the question like I typed, as it is supposed to represent the default choice so if the user presses [y], [shift + y], or [enter] it goes to the function that does the amortization table and if the user presses anything else it goes to the next line When it gets the input from the user it processes the keystroke instantly as it is pressed (it does not need [enter] to process the input) without letting the keystroke appear on the command prompt. I have tried all the functions I can think of to do this (getc, getchar, getche) but everything I have tried ends up printing the user's input. Does anyone know what function he used or what trick he is doing to keep the keystroke from appearing in the command prompt? Thanks for your help in advance, I am obviously new to programming.
For the Windows platform, use the _getch() function from <conio.h> to read a keypress without buffering or echo.
Read the function description on MSDN

Resources