所以,我试图制作一个密码脚本,它大部分都有效。然而,循环继续,而不是在每个"不正确的密码"之后停止,因此用户可以输入密码。然而,它不断发送不正确的密码,并没有任何机会输入任何内容。
我也尝试将read password
置于直行之下,但这也没有效果。
#!/bin/bash
# Author: Dom (GlitchyShadowZ)
# No name lol
# Password Protected Terminal
echo "Please enter password to continue"
str="owo"
read password
until [ $password = $str ]
do
# Password Verification
if [ $password = $str ];
then
echo "Password correct. Logging in"
else
echo "Password Incorrect. Try again."
fi
done
答案 0 :(得分:3)
每次比较之前都必须阅读密码。此外,您正在检查$password = $str
条件两次 - 没有必要。您只需检查一次,方法是从循环中完全删除if
并在脚本末尾移动Password correct. Logging in
,如下所示:
#!/bin/bash
# Author: Dom (GlitchyShadowZ)
# No name lol
# Password Protected Terminal
echo "Please enter password to continue"
str="owo"
read password
until [ "$password" = "$str" ]
do
echo "Password Incorrect. Try again."
read password
done
echo "Password correct. Logging in"
输入正确的密码后,或使用while true
和break
内部循环:
#!/bin/bash
# Author: Dom (GlitchyShadowZ)
# No name lol
# Password Protected Terminal
str="owo"
password=""
echo "Please enter password to continue"
while true
do
read password
# Password Verification
if [ "$password" = "$str" ];
then
echo "Password correct. Logging in"
break
else
echo "Password Incorrect. Try again."
fi
done
在这两种情况下,请确保将""
放在变量周围,因为当前运行脚本并且没有输入任何内容并立即按下 Enter 时,您会得到:
line 13: [: =: unary operator expected