【问题标题】:My script wont redirect to my other classes?我的脚本不会重定向到我的其他课程?
【发布时间】:2020-02-25 17:33:50
【问题描述】:

我创建了一个变量并将其分配给一个输入。但无论我输入什么,它都会重定向回:EditUser。我不知道为什么会这样。

这就是我的代码。有人可以告诉我为什么它不会直接指向其他班级。这是我脚本中唯一不起作用的重定向。

:EditUser
cls
echo You chose to edit a user
echo ========================
echo 1. Change Password
echo 2. Change Account Type
echo 3. Go back to Users
echo.
set /p CHOICE6=What do you want to do?
if CHOICE6==1 goto PassChange
if CHOICE6==2 goto PrivChange
if CHOICE6==3 goto Users
goto EditUser

【问题讨论】:

    标签: windows batch-file scripting windows-10 command-prompt


    【解决方案1】:

    你忘了用% 包装你的变量来使它们被识别出来。它应该是 %CHOICE6% 此外,您应该将 if 语句查询用双引号括起来以消除可能的空格 if "%CHOICE6%"=="1" 或者更好地看到它是数字,使用 if %CHOICE6% equ 1

    无论如何,既然您正在处理 Choice 的明显问题,那么只需使用选择而不是 set /p

    :EditUser
    cls
    echo You chose to edit a user
    choice /c 123 /m "Select (1) Change Password (2) change account type (3) go back to users"
    goto :opt%errorlevel%
    
    :opt1
    echo change password stuff here.
    goto :eof
    
    :opt2
    echo change account type stuff goes here
    goto :eof
    
    :opt3
    goto :users
    goto :eof
    

    所以真正发生的是,我们只是转到一个以opt 开头的标签,后面加上choice 返回的错误级别

    它也只允许输入一个字母数字值,您输入的任何其他内容都不会被执行。所以在这种情况下,只有 1、2 或 3 会被 cmd 操作。

    查看choice /?了解更多详情。

    另外,如果您想保留 echo 布局并且在一行中没有选项,请添加 echo 并从 choice 中删除消息 /m swirtch:

    :EditUser
    cls
    echo You chose to edit a user
    echo ========================
    echo 1. Change Password
    echo 2. Change Account Type
    echo 3. Go back to Users
    echo(
    choice /c 123
    goto :opt%errorlevel%
    
    :opt1
    echo change password stuff here.
    goto :eof
    
    :opt2
    echo change account type stuff goes here
    goto :eof
    
    :opt3
    goto :users?
    goto :eof
    

    【讨论】:

      【解决方案2】:

      您只是忘记在使用它们时将变量包装在% 中。

      但是,您最好使用将 SET 命令包装在 " 中的良好做法

      另外,你可以通过使用选择来简化你的生活,尽管仍然有可能让选择出错,所以 YMMV

      :EditUser
      cls
      echo.You chose to edit a user
      echo.========================
      echo.1. Change Password
      echo.2. Change Account Type
      echo.3. Go back to Users
      echo.
      set /p "CHOICE6=What do you want to do? "
      IF %CHOICE6% EQU 1 goto :PassChange
      IF %CHOICE6% EQU 2 goto :PrivChange
      if %CHOICE6% EQU 3 goto :Users
      goto :EditUser
      

      也就是说,为了获得最佳实践,您应该使用 CALL 编写函数,并且您可以将选择变量返回到主代码以执行操作,或者在函数中作为最佳套件执行。

      CALL 是“安全的”,因为它会在函数或脚本结束时返回到当前执行点,您可以更好地编写脚本。

      REM Example main function
      :Main
        REM ...
      
        REM Call Edit User, Specify a variable name to be returned
        CALL :EditUser "CALL_THIS"
      
        REM Call the function we chose in the :Edit User Function and was returned to us here, and may be :PassChange, :PrivChange, :Users
        CALL %CALL_THIS% 
      
        REM ...
      GOTO :EOF
      
      :EditUser
      REM Clear the Variable Provided to the fucntion to be returned
      SET "%~1="
      REM Clear the Choice
      SET "CHOICE6="
      cls
      echo.You chose to edit a user
      echo.========================
      echo.1. Change Password
      echo.2. Change Account Type
      echo.3. Go back to Users
      echo.
      CHOICE /C 123 /N /M "What do you want to do? "
      SET "CHOICE6=%ERRORLEVEL%"
      REM Check an arbitrary number of choices without having to write a full IF on each.
      FOR %%_ IN (
       1:PassChange
       2:PrivChange
       3:Users
      ) DO (
      
        REM Split choices to test them:
        FOR /F "Tokens=12 Delims=:" %%A (
          IF %%A EQU %CHOICE6% (
      
            REM Set The Variable given the function '%1' to a value to return to Main script when the Choice was matched.
            SET "%~1=:%%B"
          )
        )
      
        REM Exit Function when %~1 is defined.
        IF DEFINED %~1 GOTO :EOF
      )
      
      REM Show screen again when not matched
      GOTO :EditUser
      

      是的,还有更多代码,但正如您现在所看到的,您有一个可重用的函数,可以根据您的代码要求任意多次调用,并允许外部代码在另一个函数或主代码中执行下一个调用功能。

      但即使在您不需要调用另一个函数的地方,您也可以使用此处的循环逻辑来存储选项列表并评估它们,而无需重新键入所有 if 语句。

      此外,您也可以更通用地编写它,并且没有任何多项选择变量每次都将选择函数逻辑重新用作可调用函数,并允许您编写更少重复且占用空间更小的代码。

      【讨论】:

        猜你喜欢
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        • 2018-10-25
        • 1970-01-01
        • 1970-01-01
        • 2018-08-03
        • 1970-01-01
        相关资源
        最近更新 更多