Run a process when a string in STDOUT matches

I start my blog with Jekyll , and I thought it would be nice to automatically open the site in a new browser tab when the local development server finishes launching.

When you run jekyll serve , you have the following output, for example:

 Configuration file: /Users/jgt/Sites/jezen.imtqy.com/_config.yml Source: /Users/jgt/Sites/jezen.imtqy.com Destination: dist Generating... done. Configuration file: /Users/jgt/Sites/jezen.imtqy.com/_config.yml Server address: http://0.0.0.0:4000/ Server running... press ctrl-c to stop. 

I was thinking of running jekyll serve in a subshell and listening to its output. Perhaps I could simultaneously run a parallel endless loop that reads some stdout and runs open http://0.0.0.0:4000 : open http://0.0.0.0:4000 , and then exits when the line "Server is running" matches. My bash-fu is not yet at a level where I can hack something.

How do I approach this?

+4
source share
2 answers

You can do something like this in bash:

 (jekyll serve) | while read line; do [[ $line =~ "Server running" ]] && open http://0.0.0.0:4000 done 

Running jekyll serve in the subshell and in the stdout pipe. He then uses the bash regex match to match a string containing the words "Server is running."

+3
source

You can simply redirect stdout to a file and then pin that file, grep to your line and then run the action

 run proccess > outputfile; tail -f outputfile| grep "your string"| while read string; do open http://0.0.0.0:4000; done 
0
source

Source: https://habr.com/ru/post/953423/


All Articles