if条件,在grep中循环

if条件,在grep中循环

有人可以为我提供帮助吗?

我尝试开始编写 shell 脚本,但对这个问题感到困惑。我需要grep命令“adb shell ps”并在其中找到 3 个进程,其名称为:

  1. 进程1
  2. 过程2
  3. 过程3

如果这些进程出现在grep输出中,则继续使用脚本,否则grep在 30 秒的时间间隔内检查 5 次。最后 - 如果我的进程没有启动,只需退出表单脚本。

答案1

这是一个我认为您正在寻找的脚本:

#!/bin/bash

cnt=1; found=0;
while [ "$cnt" -le 5 ]; do
  echo "chk#: $cnt"
  if [[ $(pgrep -f "proc1") && $(pgrep -f "proc2") && $(pgrep -f "proc3") ]]; then
    found=1
    break
  fi
  let cnt=cnt+1
  sleep 6
done

[ "$found" -eq 0 ] && exit

echo "found them"

如果 proc1、proc2 和 proc3 是进程的实际名称,则可以更改此行。上面的内容在命令行中查找这些字符串的任何出现。

  if [[ $(pgrep "proc1") && $(pgrep "proc2") && $(pgrep "proc3") ]]; then

该脚本使用pgrep而不是因为这个命令基本上可以在单个命令中grep执行。ps .. | grep ...

相关内容