use*_*970 0 verification variable read
我有这个read
操作:
read -p "Please enter your name:" username
Run Code Online (Sandbox Code Playgroud)
如何在一行中验证用户名?
如果在一行中不可能以理智的方式实现,那么放在变量中的 Bash 函数可能是一个不错的解决方案?
Name 只是一个例子,它可以是密码或任何其他常见的表单值。
验证在这里的意思是:请求用户两次插入名称并确保两个值相同。
用户输入(或者,可能,复制和粘贴...)同样的事情两次通常是通过两次read
调用、两个变量和一个比较来完成的。
read -p "Please enter foo" bar1
read -p "Please enter foo again" bar2
if [ "$bar1" != "$bar2" ]; then
echo >&2 "foos did not match"
exit 1
fi
Run Code Online (Sandbox Code Playgroud)
这可以通过while
循环和条件变量来完成,重复提示和检查直到匹配为止,或者如果将有很多输入提示,则可能抽象为函数调用。
归档时间: |
|
查看次数: |
68 次 |
最近记录: |