【发布时间】:2022-01-14 04:39:31
【问题描述】:
我正在尝试编写一个脚本来帮助我在日志中搜索短语“started”。
到目前为止,我的脚本如下所示:
#!/bin/bash
greperg=
i=3
echo "Web server is going to be polled"
while
i=`expr $i - 1`
#polling
echo "Polling Nr. $i"
grep -q '^started$' log
greperg=$?
#test, if it goes on
test "$greperg" -gt 0 -a "$i" -gt 0
do
#waiting
echo 'waiting ...'
sleep 1
done
if test "$greperg" -eq 0
then
echo "Web server has started"
else
echo -n "Web server is not started"
fi
valid=''
while ((!valid)); do
echo - "Do you want to poll again? (J/N)"
read -t 5 answer
case "$answer" in
[Jj]) result=1; valid=1;;
[Nn]) result=0; valid=1;;
"") result=0; valid=1;;
*) valid=0 ;;
esac
done
echo
if ((result));then
: # ...............(repeat the process again, if its not found ask max 5 times)
else
echo "Timeout"
fi
exit 0
从第 38 行开始,我不知道如何重新运行它,有人可以帮忙吗?
我在寻找什么:
-
应该扩大轮询:如果在 3 次尝试之后该词 (started) 仍然不存在,则使用 (Y / N) 查询询问用户是否应该轮询更多 3 次。
-
最多应询问用户 5 次 (所以最多轮询 3 × 6 = 18 次)。
-
最后请说明达到的状态(参见下面的示例)。
polling ... Web server has not started yet. Wait ... polling ... Web server has not started yet. Wait ... polling ... Web server has not started yet. Should it be polled again (Y / N)? _ Y polling ... Web server has not started yet. Wait ... polling ... Web server has not started yet. Wait ... polling ... Web server has not started yet. Should it be polled again (Y / N)? _ N As requested, no further attempts are made. Bottom line: web server has not started.
【问题讨论】:
-
您的代码不遵循 bash 语法,请将您的代码放入 shellcheck.net 并先修复它。将代码放在
while和do之间是不好的形式。我实际上很惊讶 shellcheck 没有将其标记为无效。而its not working不是问题,请包含完整的调试信息(参见minimal reproducible example) -
现在好点了吗?
-
是和不是。无论如何,在第 38 行,您想再次重复该过程。无论过程是什么,创建一个函数。每次需要执行代码时,调用函数。
-
对,看看我所做的更改。
-
坦率地说,无论如何都应该这样做。
标签: linux bash prompt polling script