I need to start a server kind of program (http-server), it run in the terminal and will output the port it is using when ready. I want to wait until the port number is displayed and then run another command that will also be a server kind of program (run and accept user interaction)

I can get itto almost work with

http-server | (grep -m 1 "Server runing"; interactiveProgram)

InteractiveProgram will launch after grep match the first line but it won'tbe interactive)


I suspect the problem is that with

http-server | (grep -m 1 "Server running"; interactiveProgram)

interactiveProgram’s standard input isn’t connected to the terminal, but piped from http-server’s standard output (once grep has finished with it).

http-server | (grep -m 1 "Server running"; interactiveProgram < /dev/tty)

should do what you’re after.

  • Thanks @pLumo, that is a bit of a blocker! – Stephen Kitt Nov 5 '19 at 14:15

If you run it as part of the pipe, stdin won't be connected with the terminal as @Stephen Kitt already said. But if you run it afterwards, it won't run before http-server has quit.

A workaround would be to use a while-loop with a grep call each line:

http-server | while read line; do
    echo "$line" | grep "Server running" && { interactiveProgram; break; }

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.