Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
414 views
in Technique[技术] by (71.8m points)

batch file - Password system does not work

Here is the code I am trying to solve:

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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.

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.

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

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.

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.

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

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 command interpreter does implicitly do an endlocal on exiting batch file execution on executing goto :EOF resulting in restoring previous command process environment.

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

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

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 /?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...