微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用户输入一定数量的时间 - bash 脚本

如何解决用户输入一定数量的时间 - bash 脚本

我定义了一个函数如下。

无输入-退出
输入'n'-> 退出
输入'y'->继续下一步
'y' 或 'n' 以外的输入 -> 重试 3 次仍然输入错误 -> 退出

我无法实现最后的重试部分。有人可以帮忙吗?

my_func()
  {
   read -p "Still want to continue (y/n)? : " userin
   userin=${userin,}
   if [ -z "$userin" ];then
        echo "No input"
        exit 1
   elif [ $userin == "n" ]; then
        exit 1
   elif [ $userin == "y" ]; then
        echo "Next step starting
        return
   else
        echo "You entered $userin - give proper input(y/n)"

   fi
}

解决方法

您是否会尝试以下操作:

my_func() {
     for ((i = 0; i < 3; i++)) {
         read -p "Still want to continue (y/n)? : " userin
         if [[ -z $userin ]]; then
             echo "No input"
             exit 1
         elif [[ $userin = "n" ]]; then
             exit 1
         elif [[ $userin = "y" ]]; then
             echo "Next step starting"
             return
         else
             echo "You entered $userin - give proper input(y/n)"
         fi
    }
    exit 1
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。