当STDOUT字符串匹配运行过程 [英] Run process when string in STDOUT is matched

查看:137
本文介绍了当STDOUT字符串匹配运行过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑我的博客与杰基尔,我想这会是一个不错的主意,自动打开网站在新的浏览器选项卡时,本地开发服务器完成启动。

I’m running my blog with Jekyll and I thought it’d be a nice idea to automatically open the site in a new browser tab when the local development server finishes starting up.

当您运行杰基尔服务,你有一个像下面的一些输出:

When you run jekyll serve, you have some output like the following:

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

我原以为在子shell中运行杰基尔服务并监听其输出。也许我可以在同一时间启动一个并发的无限循环,上面写着一些stdout和运行开http://0.0.0.0:4000 ,然后退出时字符串服务器运行相匹配。我的bash福还不在一个水平,我可以砍的东西在一起。

I had thought of running jekyll serve in a subshell and listening for its output. Maybe I could at the same time fire up a concurrent infinite loop that reads some stdout and runs open http://0.0.0.0:4000 and then exits when the string "Server running" is matched. My bash-fu is not yet at a level where I can hack something together.

我应该如何处理这个?

推荐答案

您可以做在bash是这样的:

You could do something like this in bash:

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

这运行杰基尔服务在子shell和管道标准输出来循环。然后,它使用正则表达式的bash匹配匹配包含服务器运行的字样就行了。

This runs jekyll serve in a subshell and pipes stdout to the loop. It then uses bash regex matching to match the line containing the words "Server running".

这篇关于当STDOUT字符串匹配运行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆