【问题标题】: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 标志:
-
-F - 按字面意思对待字符串,而不是正则表达式
-
-q - 不向标准输出打印任何内容
-
-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"