【问题标题】:Shellscript Ubuntu Find out if a user entered a specific characterShellscript Ubuntu 查明用户是否输入了特定字符
【发布时间】:2013-10-20 13:47:10
【问题描述】:

我想做一个while循环,直到用户输入ab“@”,这是我的代码,但它不起作用:

echo -n "username(email): "
read username 
checkEmail $username

checkEmail () {

username=$1
echo $username | grep "@"
while [ ! $? -eq 0 ] ; do
        echo -n  "username(email): "; read username
done
}

【问题讨论】:

    标签: string shell input while-loop


    【解决方案1】:

    首先,你必须在使用之前定义函数。此外,在 while 循环的每次迭代期间检查 - 您的代码仅在第一次迭代中检查 grep 的结果,然后检查 read 的结果。试试

    username=""
    checkEmail () {
      while echo "$username" | grep -vq "@" ; do
        echo -n "username(email): "; read username
      done
    }
    
    checkEmail
    echo "Correct email: $username"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2020-06-23
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多