2014-12-07 73 views
0

我目前创建了一个unix bash,要求输入Y/N,然后为用户输入y或n时创建了代码。但是如果用户键入其他的东西,它会出现“请输入Y或N”的回声,那么我如何重定向它以便返回到原始的Y/N输入?Unix - 针对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

哪里是你当前的代码? – 2014-12-07 13:04:33

+0

刚刚添加的代码 – james111 2014-12-07 13:10:51

回答

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 
+0

你可以使用'read -p'你想改变目录吗? Y/N'答案' – 2014-12-07 13:20:03

+0

我不完全理解编辑?为什么需要报价? – 2014-12-07 13:20:05

+0

就像我想要的那样工作。谢谢 – james111 2014-12-07 13:21:00