我如何等待流程的某些输出然后在Bash中继续? [英] How can I wait for certain output from a process then continue in Bash?

查看:42
本文介绍了我如何等待流程的某些输出然后在Bash中继续?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个bash脚本来做一些事情,启动一个进程,等待该进程说它已经准备好,然后在该进程继续运行的同时做更多的事情.我遇到的问题是找到一种方法来等待该流程准备就绪,然后再继续运行,并允许其继续运行.

I'm trying to write a bash script to do some stuff, start a process, wait for that process to say it's ready, and then do more stuff while that process continues to run. The issue I'm running into is finding a way to wait for that process to be ready before continuing, and allowing it to continue to run.

在我的特定情况下,我正在尝试建立PPP连接.我需要等到它连接好后,再运行下一个命令.如果PPP无法连接,我也想停止脚本.pppd打印到标准输出.

In my specific case I'm trying to setup a PPP connection. I need to wait until it has connected before I run the next command. I would also like to stop the script if PPP fails to connect. pppd prints to stdout.

在伪代码中,我想做的是:

In psuedo code what I want to do is:

[some stuff]
echo START

[set up the ppp connection]
pppd <options> /dev/ttyUSB0
while 1
  if output of pppd contains "Script /etc/ppp/ipv6-up finished (pid ####), status = 0x0"
    break
  if output of pppd contains "Sending requests timed out"
    exit 1

[more stuff, and pppd continues to run]
echo CONTINUING

关于如何执行此操作的任何想法?

Any ideas on how to do this?

推荐答案

我不得不做类似的事情,等待/var/log/syslog中的一行出现.这对我有用:

I had to do something similar waiting for a line in /var/log/syslog to appear. This is what worked for me:

FILE_TO_WATCH=/var/log/syslog
SEARCH_PATTERN='file system mounted'

tail -f -n0 ${FILE_TO_WATCH} | grep -qe ${SEARCH_PATTERN}

if [ $? == 1 ]; then
    echo "Search terminated without finding the pattern"
fi

它将通过管道将附加到监视文件的所有新行传送到grep,并指示grep一旦发现该模式就立即安静地退出.下面的if语句检测"wait"是否在没有找到模式的情况下终止.

It pipes all new lines appended to the watched file to grep and instructs grep to exit quietly as soon as the pattern is discovered. The following if statement detects if the 'wait' terminated without finding the pattern.

这篇关于我如何等待流程的某些输出然后在Bash中继续?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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