如果输入无效,请继续循环

时间:2014-03-05 09:49:35

标签: bash shell

我是shell脚本的新手,也是编程的初学者。我有一个场景,我想问一下。我将部分脚本粘贴在下面,它是案例切换的一部分。

echo -n "Delete Source File After Encryption : [Y|N] "
read DSFE

if [ $DSFE = "Y" -o $DSFE = "y" ]; then
  Do something;
  clear

elif [ $DSFE = "N" -o $DSFE = "n" ]; then 
  Do something;
  clear

我想知道如何对上面的代码段进行编码,这样如果用户输入不是Y或N,它将循环并提示问题。

对这个床垫有何评论?

谢谢。

此致 凯文

1 个答案:

答案 0 :(得分:1)

until [[ $DSFE == [YyNn] ]]; do
        read -p "Delete Source File After Encryption? [Y|N] " DSFE
done

case $DSFE in
        [Yy]) do_something ;;
        [Nn]) do_something_else ;;
        *)    # this code should not be reached
esac