這裏是我試圖解決代碼:密碼系統不工作
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
它是一個「迷你操作系統」批量製造。
這裏是我試圖解決代碼:密碼系統不工作
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
它是一個「迷你操作系統」批量製造。
如果用戶只是點擊RETURN或ENTER導致仍然沒有被定義爲不具有上述線的初始值與set /p
定義的環境變量password
的代碼不工作。
這導致上IF條件與環境變量password
立即膨脹,並與在命令行具有值xxx
pass
:
if == xxx goto desktop
左串丟失,因此批處理執行是因爲一個退出語法錯誤,因爲它可以在上批處理文件的頂端從命令提示窗口,而不是雙擊批處理文件內運行的批處理文件,而不@echo off
看到。
此外,如果所輸入的字符串中包含例如|
或&
或<
或>
的環境變量password
結果上解析語法錯誤的IF條件符合批處理文件執行的立即退出即時膨脹。
常見的解決辦法與解釋爲文字字符的特殊含義的字符獲得是把兩個字符串在雙引號比較。雙引號也通過IF進行比較,而不僅僅是雙引號之間的字符串。因此有必要對兩個字符串進行雙重引號。
if "%password%" == "%pass%" goto desktop
if not "%password%" == "%pass%" goto bootscreentwo
if "%password%" == "Guest" goto ltddesktop
哪些字符需要大約一個字符串雙引號在Windows命令解釋器輸出的幫助下在最後輸出的幫助頁最後一段在命令提示符窗口中運行cmd /?
解釋。在列表中缺少redirection operators>
,<
和|
,因爲這些字符不能在文件名中使用。
但是,即使在字符串周圍使用雙引號以比較批處理文件用戶是否輸入包含1個或更多雙引號的字符串仍然存在問題,因爲這會導致在執行語法錯誤時再次立即進行環境變量擴展IF命令行。
該溶液是用延遲的環境變量擴展爲在命令提示窗口set /?
運行由命令SET輸出的幫助下說明。
首先建議對其中用戶必須輸入guest
使用訪客訪問批代碼:
@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命令解釋程序暗中做一個endlocal
上執行goto :EOF
導致恢復以前退出批處理文件執行命令進程環境。
第二個建議批次代碼guest
是默認密碼:
@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行中使用!
時要小心,因爲感嘆號必須轉義以解釋爲文字字符。如果在此批次代碼塊之後不再需要password
和pass
的值,那麼在進一步處理批處理文件之前最好使用endlocal
。
@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
和password
不再被定義的字符串進行比較後,除兩個環境變量在第二線SETLOCAL命令在這種情況下,它們的原始值由ENDLOCAL恢復執行前已經存在。有關命令的詳細信息,請參閱this answerSETLOCAL和ENDLOCAL。
爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。
echo /?
endlocal /?
goto /?
if /?
rem /?
set /?
setlocal /?
確實,這是一些代碼。你能詳細說明「不起作用」是什麼意思嗎?否則,這將很難幫助你。 – KernelPanic
當你的代碼看起來像是「使用延遲擴展」和「在%password%和%pass%周圍加上引號」時,傳統的對「不起作用」的迴應是這樣的,但老實說,已發佈。 – SomethingDark