密码系统不起作用 [英] Password system does not work

查看:65
本文介绍了密码系统不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解决的代码:

set /p password= Password (Put in Guest if guest login):
if %password% == %pass% goto desktop
if not %password% == %pass% goto bootscreentwo
if %password% == Guest goto ltddesktop
:desktop

它是批量生产的小型操作系统".

it is for a "Mini operating system" made in batch.

推荐答案

如果用户只是按下 RETURN ENTER 导致环境变量仍未定义为未在set /p行上方使用初始值进行定义.

The code does not work if the user just hits RETURN or ENTER resulting in environment variable password still not being defined as not defined with an initial value above line with set /p.

这是在 IF 条件下,环境变量password立即扩展,并且pass在命令行中具有值xxx的结果.

This results on IF condition with immediate expansion of environment variable password and with pass having value xxx in the command line:

if == xxx goto desktop

缺少左字符串,因此由于语法错误而退出了批处理执行,这是在运行批处理文件时看到的,而该批处理文件是在命令提示符窗口中而不是双击该批处理文件的顶部而不使用@echo off的情况下看到的批处理文件.

The left string is missing and therefore batch execution is exited because of a syntax error as it can be seen on running the batch file without @echo off at top of the batch file from within a command prompt window instead of double clicking on the batch file.

此外,如果输入的字符串包含例如|&<>,则在解析 IF 条件行时会导致环境变量password的立即扩展语法错误并立即退出批处理文件执行.

Also if the entered string contains for example | or & or < or > the immediate expansion of environment variable password result on parsing the IF condition line in a syntax error with an immediate exit of batch file execution.

将具有特殊含义的字符解释为文字字符的常见解决方案是将两个字符串都放在双引号中进行比较. IF 还会比较双引号,而不仅仅是双引号之间的字符串.因此,必须将两个字符串都用双引号引起来.

The common solution to get characters with special meaning interpreted as literal characters is putting both strings to compare in double quotes. The double quotes are also compared by IF and not just the string between the double quotes. Therefore it is necessary to double quote both strings.

if "%password%" == "%pass%" goto desktop
if not "%password%" == "%pass%" goto bootscreentwo
if "%password%" == "Guest" goto ltddesktop

在Windows命令解释器输出的帮助下,通过在最后一个输出帮助页面上的最后一段中的命令提示符窗口cmd /?中运行,来解释哪些字符需要用双引号引起来. 重定向运算符 ><|此列表中缺少这些字符,因为这些字符不能在文件名中使用.

Which characters require double quotes around a string is explained in help of Windows command interpreter output by running in a command prompt window cmd /? in last paragraph on last output help page. The redirection operators >, < and | are missing in this list as those characters can't be used in file names.

但是,即使批处理文件用户输入包含1个或多个双引号的字符串,即使在字符串周围使用双引号进行比较,仍然存在问题,因为这会导致立即环境变量再次扩展,导致执行<时出现语法错误strong> IF 命令行.

But there is still a problem even on using double quotes around the strings to compare if the batch file user enters a string containing 1 or more double quotes as this results with immediate environment variable expansion again in a syntax error on execution of IF command line.

该解决方案使用延迟的环境变量扩展,如在命令提示符窗口set /?中运行时输出的命令 SET 所说明的那样.

The solution is using delayed environment variable expansion as explained by help of command SET output on running in a command prompt window set /?.

关于用户必须在何处输入guest才能使用来宾访问权限的批处理代码的第一个建议:

First suggestion for the batch code on where the user has to enter guest for using guest access:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "pass=xxx"

:EnterPassword
set "password="
set /P "password=Enter "Guest" for guest login. Password: "
if "!password!" == "" goto EnterPassword

rem Case-sensitive password string compare using delayed expansion.
if "!password!" == "!pass!" goto desktop

rem Case-insensitive password string compare using delayed expansion.
if /I "!password!" == "Guest" goto ltddesktop

echo bootscreentwo
goto :EOF

:desktop
echo Label desktop
goto :EOF

:ltddesktop
echo Label ltddesktop
goto :EOF

Windows命令解释器会在执行goto :EOF时退出批处理文件执行时隐式执行endlocal,从而恢复先前的命令处理环境.

Windows command interpreter does implicitly do an endlocal on exiting batch file execution on executing goto :EOF resulting in restoring previous command process environment.

第二个批处理代码建议,以guest作为默认密码:

Second suggestion for batch code with guest being default password:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "pass=xxx"

:EnterPassword
set "password=guest"
set /P "password=Press ENTER for guest login. Password: "

rem Case-insensitive password string compare using delayed expansion.
if /I "!password!" == "Guest" goto ltddesktop

rem Case-sensitive password string compare using delayed expansion.
if "!password!" == "!pass!" goto desktop

echo bootscreentwo
goto :EOF

:desktop
echo Label desktop
goto :EOF

:ltddesktop
echo Label ltddesktop
goto :EOF

请注意,启用延迟扩展会导致将批处理代码中任何地方的!解释为环境变量引用的开始.因此,例如在 ECHO 行中使用!时要小心,因为感叹号必须转义才能解释为文字字符.如果在此批处理代码块之后不再需要passwordpass的值,则最好在进一步处理批处理文件之前使用endlocal.

Please note that enabled delayed expansion results in interpreting ! anywhere in batch code as beginning of an environment variable reference. So be careful on using ! for example in an ECHO line as the exclamation mark must be escaped to be interpreted as literal character. It would be best to use endlocal before further processing the batch file if the values of password and pass are no longer needed after this batch code block.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "pass=xxx"

:EnterPassword
set "password=guest"
set /P "password=Press ENTER for guest login. Password: "

rem Case-insensitive password string compare using delayed expansion.
if /I "!password!" == "Guest" endlocal & goto ltddesktop

rem Case-sensitive password string compare using delayed expansion.
if "!password!" == "!pass!" endlocal & goto desktop

endlocal
echo bootscreentwo with pass and password not defined anymore.
goto :EOF

:desktop
echo Label desktop with pass and password not defined anymore.
goto :EOF

:ltddesktop
echo Label ltddesktop with pass and password not defined anymore.
goto :EOF

在比较字符串之后,不再定义

passpassword,除了在第二行执行 SETLOCAL 命令之前已经存在的两个环境变量,在这种情况下,它们的原始值将通过以下方式恢复: ENDLOCAL .有关命令 SETLOCAL ENDLOCAL 的详细信息,请参见此答案.

pass and password are not defined anymore after the string compares, except those two environment variables existed already before execution of SETLOCAL command in second line in which case their original values are restored by ENDLOCAL. See this answer for details about the commands SETLOCAL and ENDLOCAL.

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

这篇关于密码系统不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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