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.

  • Not very professional, but just for testing give a try to something like : ./application -login user@192.168.65.10; sleep 5 && echo "getstats" &; sleep 10 && echo "exit" &; – George Vasiliou Feb 20 '17 at 9:52
  • Please specify what language the application is written in. – agc Feb 22 '17 at 21:40
  • @agc Updated. It is developed in C – sphchow Feb 24 '17 at 7:59
2

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.

  • 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 8: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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.