Bash:我可以在If Branch之外循环命令吗?

时间:2018-02-18 22:21:46

标签: bash while-loop do-while

我正在处理一个bash脚本,我在使用while-loop.时遇到了一些问题。下面是我需要帮助的代码部分。

#!/bin/bash
set -e

    # *start*
    read -rp  "  written by: " bit
    tput sgr 0
    cat /home/sir/rmf/posted | egrep -wi --color "${bit,,}" || echo "..."F
    tput setaf 9
    read -rp "  been posted? " repeat
        if [[ $repeat == "no" ]]; then
            tput setaf 2
            echo "let's get this post started."
            post=/home/sir/rmf/active
            cat /home/sir/rmf/header > $post
            tput setaf 13
                 read -rp "  was that they full name? " bit2
                      if [[ $bit2 == "yes" ]]; then
                           author=$bit
                       else
                            read -a author -rp "  well? "
                            author=${author[@]}
                        fi
                        # exit and continue
         elif [[ $repeat == "yes" ]]; then
              echo "alright. "
              # loop back to *start*
         fi

echo "you are now outside the loop."
tput sgr 0
# and continue on to the next part

我把循环放在几个不同的地方(不知道我是否还需要添加它)但是脚本退出或者卡在其中但是我无法让它回到*start*

我将while true, dobreak和/或continue放在何处,以便$repeat == "yes"代码循环回read -rp "  written by: " bit,直到$repeat == "no" }?

1 个答案:

答案 0 :(得分:0)

#! /bin/bash

while : ; do
    echo Looping...
    unset repeat
    until [[ $repeat == @(yes|no) ]] ; do
        read -rp 'Repeat?' repeat
    done
    if [[ $repeat == no ]] ; then
        break
    fi
done

echo Done.