1

I have developed an application in C that is a command line. The app has special keywords to output information. Currently I need to run a command to log into my application, than run a command to get an output, and finally exit the application.

Is there a way to combine my 3 lines into one? For Example, login, enter command, and exit sequentially? As you can see in the example below, if I can do this all in one line I wouldn't need to manually input every step.

./application -login user@192.168.65.10
> getstats
> exit    
./application -login user@192.168.65.11
> getstats
> exit
etc...

What I want is something like...

./application -login user@192.168.65.10 && getstats && exit

Of course && is not what I'm looking for as it runs each command one at a time until. getstats and exit don't get run inside the .application but rather after I exit out of it...

Any tips would be great.

sphchow
  • 361

1 Answers1

3

If this application works with standard input, this may work:

printf 'getstats\nexit\n' | ./application -login user@192.168.65.10

If that doesn't work, you can try an expect script. You can see an example here.

agc
  • 7,223
  • I tried this method but session seems to hang. The system I am using doesn't have expect installed and I'd prefer a method that doesn't require a external application. – sphchow Feb 20 '17 at 08:59
  • @sphchow if this doesn't work expect is probably your only other choice. – terdon Feb 20 '17 at 15:16
  • @sphchow, The application is written in C. I'd suggest reconsidering which C function is used to input these command words, (i.e. getstats & exit) -- some C input functions work well with STDIN, perhaps the application can be modified to use one of those STDIN friendly functions. – agc Feb 24 '17 at 13:27