2013-03-04 37 views
4

我需要以下一些幫助:bash:通過procces輸出循環並終止進程

我使用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 

回答

5
adb shell logcat | grep TestProcess | while read line 
do 
    echo "$line" 
    if [ "$line" = "TestProces test service stopped" ] 
    then echo "TestService finished" 
     break 
    fi 
done 
+0

它的工作原理非常感謝您的幫助! – Nikl 2013-03-05 13:36:17

1
adb shell logcat | grep -Fqm 1 "TestProcess test service stopped" && echo "Test Service finished" 

grep標誌:

  1. -F - 從字面上對待字符串,而不是作爲一個正則表達式
  2. -q - 不打印任何標準輸出
  3. -m 1 - 停止第一場比賽,如果grep找到匹配的命令後

&&只執行之後。只要你「知道」grep最終會匹配,並希望無條件繼續一次它返回,只是離開&& ...

+0

工作得很好,非常感謝您的貢獻,非常感謝! – Nikl 2013-03-05 13:37:55

0

你可以使用一個until循環。

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