【问题标题】:bash: loop through procces output and terminate the processbash:遍历procces输出并终止进程
【发布时间】:2013-02-18 20:05:00
【问题描述】:

我需要以下方面的帮助:

我使用 linux 编写发送到设备的命令。我需要向设备提交一个 grep logcat 命令,然后在生成它时迭代它的输出并查找特定的字符串。找到此字符串后,我希望我的脚本移至以下命令。

伪代码

for line in "adb shell logcat | grep TestProccess"
do 
    if "TestProccess test service stopped" in line:
       print line
       print "TestService finished \n"
       break
    else:
       print line
done

【问题讨论】:

    标签: linux bash command ubuntu-12.04 output


    【解决方案1】:
    adb shell logcat | grep TestProcess | while read line
    do
      echo "$line"
      if [ "$line" = "TestProces test service stopped" ]
      then echo "TestService finished"
           break
      fi
    done
    

    【讨论】:

      【解决方案2】:
      adb shell logcat | grep -Fqm 1 "TestProcess test service stopped" && echo "Test Service finished"
      

      grep 标志:

      1. -F - 按字面意思对待字符串,而不是正则表达式
      2. -q - 不向标准输出打印任何内容
      3. -m 1 - 在第一场比赛后停止

      && 之后的命令仅在grep 找到匹配项时执行。只要您“知道”grep 最终会匹配并希望在它返回后无条件地继续,就不要使用 && ...

      【讨论】:

      • 效果很好,非常感谢您的贡献,非常感谢!
      【解决方案3】:

      你可以使用一个直到循环。

      adb shell logcat | grep TestProccess | until read line && [[ "$line" =~ "TestProccess test service stopped" ]]; do
       echo $line;
      done && echo -n "$line\nTestService finished" 
      

      【讨论】:

        猜你喜欢
        • 2019-11-05
        • 1970-01-01
        • 1970-01-01
        • 2019-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        相关资源
        最近更新 更多