除非用户的输入变量来匹配 [英] Until user input matches variable do

查看:122
本文介绍了除非用户的输入变量来匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我试图建立某种形式的用户名/密码登录脚本。 (未必是最安全的想法,我仍然工作就可以了);)

Okay, so I'm trying to create a username/password login script of sorts. (may not be the most secure idea I'm still working on it) ;)

我的脚本将加载变量从这样的文件进行比较。 (现在我只是工作的一部分密码)

My script will load variables to compare to from file like this. (right now I'm just working on password portion)

./path/to/variables.conf

这个文件将包含一个名为变量

This file will contain a variable called

PASS=SOME_VALUE 

我打算使用读来获取将被比较的变量

I plan to use read to obtain the variable that will be compared

read -p "Enter your password:" CPASS; 

现在我缺少部分(我是如何设想它的工作)

Now the part I'm missing (how I envision it working)

while "$CPASS" doesn't match "$PASS" do 
read -p "Wrong password, try again:" CPASS;

感谢您和放大器;任何帮助是AP preciated。

Thank you & any help is appreciated.

推荐答案

这应该这样做。你只需要 [] 括号和 = 运营商的比较在bash 字符串:

This should do it. You just need [ ] braces and the != operator to compare strings in bash:

PASS=SOME_VALUE

read -p "Enter your password:" CPASS

while [ "$CPASS" != "$PASS" ]; do 
    read -p "Wrong password, try again:" CPASS
done

另外请注意这将是非常可取的传递 -s 参数来读取,从而使输入的密码不回显给用户。从男人庆典部分:

Also note it would be highly advisable to pass the -s parameter to read, so that the entered password is not echoed back to the user. From the read section of man bash:

          -s     Silent  mode.  If input is coming from a terminal, char-
                 acters are not echoed.

请注意,虽然有也不会换行回显到用户,当用户点击回车,所以你必须手动插入后,每一个新行等等该行格式正确。所以,你应该像这样替换你读的语句:

Note though that there will also be no newline echoed back to the user when the user hits ENTER, so you'll have to manually insert a newline after every read so that lines are properly formatted. So you should probably replace your read statements with something like this:

read -s -p "Enter your password:" CPASS
echo

这篇关于除非用户的输入变量来匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆