【问题标题】:How to wait for text in bash shell output? [duplicate]如何在 bash shell 输出中等待文本? [复制]
【发布时间】:2021-09-12 20:42:13
【问题描述】:

我正在 bash shell 中执行命令,需要等到命令回显字符串。这是我想到的伪代码。我应该将currentline 设置为什么,以使until 循环在the text output I'm waiting for 回显时退出?

currentline=""
until [[$currentline | grep -m 1 "the text output I'm waiting for"]]; do
  echo " == Waiting for the text == "
  currentline=??? 
  sleep 1;
done

EDITthe text output I'm waiting for 告诉脚本执行的命令已准备好接收输入。该命令在the text output I'm waiting for 之后不会终止。我希望脚本停止休眠并继续。

EDIT #2:脚本在远程 CI/CD 服务 Codemagic 上运行,所以我认为我不能使用 tail 或任何需要日志文件路径的命令? (如here所述)

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/a/23930217/12957340
  • 我会考虑改用 expect。
  • edit您的问题提供一个示例,说明在命令输出中找到该字符串后您想要执行的操作,并说明您是否要在每次字符串出现在输出或只是第一次或其他。
  • @EdMorton 好的,我编辑了我的 OP

标签: bash shell grep


【解决方案1】:

就这样:

while IFS= read -r line; do
    if [[ "$line" == "the text .... " ]]; then
          break;
    fi
done < <(your-cmd)

如果您想保持您的-cmd 运行,请使用 coproc。

coproc your-cmd

while ..... as above ...
done <&"${COPROC[0]}"

【讨论】:

  • 嘎。不幸的是,我使用的是不支持 coproc 的远程 Mac Pro
  • 然后创建两个fifo并在后台运行命令,完成后记得杀死它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 2013-07-10
相关资源
最近更新 更多