如何通过 yes/no 提示重新 运行 while 循环,以搜索文件直到它出现
how to re-run a while loop by yes/no prompt, to search a file until it apears
我正在尝试编写一个脚本来帮助我在日志中搜索短语“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 通常根本不需要使用 expr
(shell 具有用于整数算术和子字符串匹配的内置工具)并且您通常希望避免使用 。我会将其分解为“分而治之”问题的函数 space.
#!/bin/bash
# Print diagnostics to standard error; include script name
echo "[=10=]: Web server is going to be polled" >&2
status=2
poll () {
local i
for ((i=1; i<=; ++i)); do
# Move wait to beginning of loop
if (($i > 1)); then
echo "[=10=]: waiting ..." >&2
sleep 1
fi
echo "[=10=]: Polling Nr. $i" >&2
# Just check
if grep -q '^started$' log; then
# return success
return 0
fi
done
# If we fall through to here, return failure
return 1
}
again () {
while true; do
# Notice read -p for prompt
read -t 5 -p "Do you want to poll again? (J/N)" answer
case "$answer" in
[Jj]*) return 0;;
"") continue;;
*) return 1;;
esac
done
}
while true; do
if poll 3
then
echo "[=10=]: Web server has started" >&2
status=0
else
echo "[=10=]: Web server is not started" >&2
status=1
fi
again || break
done
# Actually return the status to the caller
exit "$status"
主脚本中的 while true
循环可以很容易地适应 for
循环,就像在 poll
函数中一样,如果您想限制允许用户使用的次数重新开始投票。我想展示两种不同的设计,只是为了展示可供您选择的选项。
在实际脚本中,我可能会用 this || that
shorthand 替换几个简单的 if
测试。简而言之,
this && that || other
大致相当于
if this; then
that
else
other
fi
不同的是,如果 that
失败,你也会在 shorthand 的情况下触发 other
。
也许还注意到 Bash 中的 ((...))
是一个算术上下文。三位 for
循环也是 Bash 扩展。
我正在尝试编写一个脚本来帮助我在日志中搜索短语“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 通常根本不需要使用 expr
(shell 具有用于整数算术和子字符串匹配的内置工具)并且您通常希望避免使用
#!/bin/bash
# Print diagnostics to standard error; include script name
echo "[=10=]: Web server is going to be polled" >&2
status=2
poll () {
local i
for ((i=1; i<=; ++i)); do
# Move wait to beginning of loop
if (($i > 1)); then
echo "[=10=]: waiting ..." >&2
sleep 1
fi
echo "[=10=]: Polling Nr. $i" >&2
# Just check
if grep -q '^started$' log; then
# return success
return 0
fi
done
# If we fall through to here, return failure
return 1
}
again () {
while true; do
# Notice read -p for prompt
read -t 5 -p "Do you want to poll again? (J/N)" answer
case "$answer" in
[Jj]*) return 0;;
"") continue;;
*) return 1;;
esac
done
}
while true; do
if poll 3
then
echo "[=10=]: Web server has started" >&2
status=0
else
echo "[=10=]: Web server is not started" >&2
status=1
fi
again || break
done
# Actually return the status to the caller
exit "$status"
主脚本中的 while true
循环可以很容易地适应 for
循环,就像在 poll
函数中一样,如果您想限制允许用户使用的次数重新开始投票。我想展示两种不同的设计,只是为了展示可供您选择的选项。
在实际脚本中,我可能会用 this || that
shorthand 替换几个简单的 if
测试。简而言之,
this && that || other
大致相当于
if this; then
that
else
other
fi
不同的是,如果 that
失败,你也会在 shorthand 的情况下触发 other
。
也许还注意到 Bash 中的 ((...))
是一个算术上下文。三位 for
循环也是 Bash 扩展。