回答是否,重复回音

时间:2018-05-09 11:03:14

标签: linux bash if-statement while-loop

我对Bash脚本非常陌生,所以如果问题有点不连贯,请原谅。

如果用户回答“否”,我希望我的脚本重复4次问题,如果用户回答“是”,那么脚本可以退出,这是我到目前为止

#!/bin/bash
echo "Would you like a cup of tea?"

read answer

while true;

do

        if [ $answer = Y ] then
        echo "Great, I'll make tea now"; then
                break
        if [ $answer = N ] then
        echo "Are you sure?"
        continue
        if [ $answer = N ] then
        echo "Are you sure?"
        continue
        if [ $answer = N ] then
        echo "Are you sure?"
        continue
        if [ $answer = N ] then
        echo "Ok, I give up!"
        exit
fi

1 个答案:

答案 0 :(得分:0)

您需要在代码中修复许多内容。我强烈建议您在继续之前先阅读基础教程。

无论如何,你可以做的非常基本的程序是:

count=0
while true;
do
    read -r answer
    if [ "$answer" = "Y" ]; then
        echo "Great, I'll make tea now"
        break
    fi
    if [ "$answer" = "N" ]; then
        echo "Are you sure?"
        count=$((count+1))
        if [ $count = 4 ]; then
            break
        fi
    fi
done

我们将count设置为用户提供“N”作为答案的次数,我们检查它何时达到4,然后休息。