【问题标题】:how to re-run a while loop by yes/no prompt, to search a file until it apears如何通过是/否提示重新运行while循环,搜索文件直到它出现
【发布时间】: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 并先修复它。将代码放在whiledo 之间是不好的形式。我实际上很惊讶 shellcheck 没有将其标记为无效。而its not working 不是问题,请包含完整的调试信息(参见minimal reproducible example
  • 现在好点了吗?
  • 是和不是。无论如何,在第 38 行,您想再次重复该过程。无论过程是什么,创建一个函数。每次需要执行代码时,调用函数。
  • 对,看看我所做的更改。
  • 坦率地说,无论如何都应该这样做。

标签: linux bash prompt polling script


【解决方案1】:

您的代码有几个奇怪的设计。 Bash 通常根本不需要使用expr(shell 具有用于整数运算和子字符串匹配的内置工具)并且您通常希望避免使用explicitly testing $?。我会将其分解为“分而治之”问题空间的功能。

#!/bin/bash

# Print diagnostics to standard error; include script name
echo "$0: Web server is going to be polled" >&2

status=2

poll () {
    local i
    for ((i=1; i<=$1; ++i)); do
        # Move wait to beginning of loop
        if (($i > 1)); then
            echo "$0: waiting ..." >&2
            sleep 1
        fi

        echo "$0: 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 "$0: Web server has started" >&2
        status=0
    else
        echo "$0: 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 函数中一样。我想展示两种不同的设计,只是为了展示您可以使用的选项。

在一个真实的脚本中,我可能会将几个简单的if 测试替换为this || that 速记。简而言之,

this && that || other

大致相当于

if this; then
   that
else
   other
fi

不同的是,如果that 失败,您还将在速记情况下触发other

也许还要注意 Bash 中的 ((...)) 是一个算术上下文。三位 for 循环也是 Bash 扩展。

【讨论】:

  • 循环运行read -t 5 有点烦人。也许最好只取消超时。
  • 非常感谢三重奏 :),你的回答对我帮助很大,但我试图完成的是一个带有是/否提示的 while 循环,当是时再做 3 次,然后再问是/no(最多 5 次询问)并通过输入其他字母或回答来询问相同的问题,通过输入 No,进程应该与 timeout 一起完成。 for 循环效率很高,但不知何故我很有趣,顺便说一句,你的鳕鱼通过输入 N 有一个无限循环。
  • 我不确定你所说的无限循环是什么意思。轮询3次,询问是否再次轮询;如果我输入 N 它退出。就像答案已经解释的那样,如果您愿意,应该很容易将其重构为执行外循环五次而不是无限次,因为代码的其他部分已经演示了如何执行此操作。如果你实在想不通,(接受这个答案,或者发布你自己的答案并接受这个答案,并且)就该特定问题提出一个新问题。
  • 我的意思是,我想用一个while命令来做,你的代码循环是完美的,很棒,我只需要我现在写的方式:(
  • 如果您无法将答案应用于您的问题,也许 Stack Overflow 不适合您。改变什么不明显吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-21
  • 1970-01-01
  • 2016-10-10
  • 2021-09-03
  • 2017-07-02
  • 1970-01-01
相关资源
最近更新 更多