Pete Baughman
Pete Baughman

Reputation: 3034

Prepend to stdin

I've got an interactive command line program that we'll call program.exe. While it's running, I can type in a bunch of different commands that do different things. We'll call them: doA, doB, doC, and quit. Now, as it turns out, the first thing I want to do in program.exe is always doA. I'd like to save a few keystrokes and automatically have program.exe run the command doA upon startup. Unfortunately, I can't just pass "doA" on the command line because whoever created program.exe didn't implement that feature.

The way I see it, I have a few options:

echo doA | program.exe

This works, in that program.exe executes doA, but then I can't type any other commands to program.exe. Echo is hogging up program.exe's stdin

more | echo doA | program.exe

This also works and gets a little closer to what I'm after. program.exe executes doA and I see the output from doA. I can also type in additional commands, but I don't see the output from these commands. Furthermore, when I send the quit command, program.exe terminates (I guess), but I'm still stuck in the "more" command and I can keep typing lines until I hit ctrl+c.

The Question: Is there another way to do what I'm trying to do with a bat file? Conceptually, I think I need to combine two streams (the keyboard, and the literal "doA") into one and pass that into program.exe, but I could be way off.

Upvotes: 2

Views: 345

Answers (1)

wmz
wmz

Reputation: 3685

First, more | echo doA | program.exe does not do what you think it does. After sending doA echo does not connect those pipe stages together, it rather creates perfect sink - that's why you do not see your commands echoed. They go nowhere and are not executed by program.exe. I don't think there is a simple way to achieve what you want directly from command line, but it can be easily resolved by using small batch file. Create a batch file, say args.bat:

@echo off 
echo First command
more

Now run your program by args|program.exe

Upvotes: 2

Related Questions