在shell脚本中出现while循环

时间:2017-04-23 08:21:30

标签: bash shell while-loop break

我有以下代码

echo "Please Enter the Email Id"
while read -r email
do
if
    echo "$email" | grep -v   "awt"
    then
    echo "You entered a invalid email id please try again"
    break
    fi
    echo "================You chose Email id as $email"
done

echo "Please Enter the user Name"
read -r name
    echo "you entered $name"
echo Final email id $email
echo final name     $name 

当我试图执行上面的代码时,我无法进入第二个条件,即它不会出现在循环中。 我需要的是它应该询问,直到用户输入正确的电子邮件ID,然后它应该转到下一组代码,最后打印最终的echo语句

1 个答案:

答案 0 :(得分:0)

你可以这样做:

while :; do
   read -rp 'Please Enter the Email Id: ' email
   if [[ $email != *awt* ]]; then
      echo "You entered an invalid email id please try again"
   else
      break
   fi
done