bash 直到用户输入等于某事做
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19526353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Until user input equals something do
提问by Geofferey
So somebody showed me how to use condition(s) to test if a user had typed input for a password. I wanna take a their example a step further and use a loop (at least thats what I think it's call).
所以有人向我展示了如何使用条件来测试用户是否输入了密码。我想更进一步地举一个他们的例子并使用一个循环(至少我认为这就是所谓的)。
Here is their example:
这是他们的例子:
read -s -p "Enter new password: " NEWPASS
if test "$NEWPASS" = ""; then
echo "Password CAN NOT be blank re-run sshd_config"
exit 1;
fi
Instead of exiting the script I want it to keep asking for the input until there is some.
而不是退出脚本,我希望它继续要求输入,直到有一些。
I wanna make a statement like this but I was doing it wrong: (Now using top example)
我想发表这样的声明,但我做错了:(现在使用顶级示例)
The user is given a chance to enter new password and fill the variable value.
用户有机会输入新密码并填写变量值。
read -s -p "Enter new password:" NEWPASS
echo ""
The next portion checks the variable to see if it contains a value, while the value is null it request the user to fill the value indefinitely. ( a loop)
下一部分检查变量以查看它是否包含值,当值为 null 时,它请求用户无限期地填充该值。( 一个循环)
while [[ -z "$NEWPASS" ]]; do
echo ""
echo "Password CAN NOT be blank"
echo ""
read -s -p "Enter new password:" NEWPASS;
echo ""
done
This line searches a file containing a set of variables used by another file. It then searches the file for a line containing PASS=.* (.* meaning anything) then writes PASS=$NEWPASS ($NEWPASS being variable)
此行搜索包含另一个文件使用的一组变量的文件。然后在文件中搜索包含 PASS=.* (.* 表示任何内容)的行,然后写入 PASS=$NEWPASS ($NEWPASS 是可变的)
sed -i -e"s/^PASS=.*/PASS=$NEWPASS/" /etc/sshd.conf
Thank you for all the help, I'm going to use this and learn from it.
感谢您的所有帮助,我将使用它并从中学习。
回答by anubhava
This while loop should work:
这个while循环应该可以工作:
while [[ -z "$NEWPASS" ]]
do
read -s -p "Enter new password: " NEWPASS
done
回答by Michael Krelin - hacker
while read -s -p 'Enter new password: ' NEWPASS && [[ -z "$NEWPASS" ]] ; do
echo "No-no, please, no blank passwords!"
done