Unix - 使用bash进行Y / N回答的问题

时间:2014-12-07 13:03:12

标签: unix

我目前创建了一个unix bash,它要求输入Y / N,然后我创建了用户输入y或n的代码。但是,如果用户输入其他内容,则会产生#34的回声;请输入Y或N"我如何重定向它,所以回到原始的Y / N输入?

 #! /bin/bash

    echo "Do you want to change the directory? Y/N?"
    read answer
    if [[ $answer == "y" || $answer == "Y" ]]; then
    echo "Yes"
    elif [[ $answer == "n" || $answer == "N" ]]; then
    echo "No"
    else
    echo "Please enter Y or N"
    #redirect back to "Do you want to change the directory" echo
    fi

1 个答案:

答案 0 :(得分:1)

这是一个解决方案:

#!/bin/bash

to_do=true

while $to_do;
do
    read -p "Do you want to change the directory? Y/N " answer
    case $answer in
        [Yy]*)
            echo Yes
            to_do=false
            ;;
        [Nn]*)
            echo No
            to_do=false
            ;;
        *)
            echo Please enter Y or N
    esac
done
相关问题