此问题在此处已有答案:
Variables are not behaving as expected(1个答案)
2天前关闭。
我做了一个批处理脚本,询问用户是否想做某件事,他们要么提供y/n。如果用户提供“y”,它将前进,如果他们提供“n”,脚本将退出。有时当提供“y”,脚本退出无论如何。我不知道如何解决这个问题,因为我还没有看到任何其他人有这个问题。以下是我的脚本:
@echo off
:check_connection
cls
echo Checking for internet connection...
ping -n 1 google.com >nul 2>&1
if not %errorlevel% == 0 (
echo No internet connection detected. Waiting 15 seconds before trying again.
timeout /t 15 >nul 2>&1
goto check_connection
) else (
echo Internet connection detected. Proceeding with script.
)
:check_nodejs
echo Checking if Node.js is installed...
where npm >nul 2>&1
if %errorlevel% == 0 (
echo Node.js is already installed.
goto check_replugged
) else (
echo Node.js is not installed on this system.
set /p install="Do you want to install it now? (y/n) "
if /i "%install%" == "y" (
goto install_nodejs
) else (
echo Exiting script.
goto :eof
)
)
:install_nodejs
echo Downloading Node.js installer...
bitsadmin /transfer "nodejs_installer" /download /priority high "https://nodejs.org/dist/v18.13.0/node-v18.13.0-x64.msi" "%temp%\node-v18.13.0-x64.msi"
echo Installing Node.js...
start "" /wait msiexec /i "%temp%\node-v18.13.0-x64.msi" /qn
if %ERRORLEVEL% == 0 (
echo Node.js installation successful.
goto check_nodejs
) else (
echo Node.js installation failed. Exiting script.
start "" "https://nodejs.org/en/download/"
goto :eof
)
:check_replugged
echo Checking if replugged is installed...
cd %userprofile%\replugged
if not exist "%userprofile%\replugged" (
set /p install="replugged is not installed on this system. Do you want to install it now? (y/n) "
if /i "%install%" == "y" (
goto install_replugged
) else (
echo Exiting script.
goto :eof
)
) else (
echo replugged is already installed.
goto check_discord
)
:install_replugged
echo Installing replugged...
cd %userprofile%
if not exist "%userprofile%\replugged" (
git clone https://github.com/replugged-org/replugged
)
cd replugged pnpm i && pnpm build
if not %errorlevel% == 0 (
echo Failed to install replugged. Exiting script.
goto :eof
) else (
echo replugged installation successful.
goto check_discord
)
:check_discord
set /p discordversion="Which discord version you want to install replugged on? (stable,ptb,canary,development) [stable]: " /t 20
if "%discordversion%" == "" (set discordversion=stable)
if /i "%discordversion%" == "stable" (
echo Stopping Discord process...
taskkill /f /im discord.exe >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to stop Discord process.
)
) else if /i "%discordversion%" == "ptb" (
echo Stopping DiscordPTB process...
taskkill /f /im discordptb.exe >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to stop DiscordPTB process.
)
) else if /i "%discordversion%" == "canary" (
echo Stopping DiscordCanary process...
taskkill /f /im discordcanary.exe >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to stop DiscordCanary process.
)
) else if /i "%discordversion%" == "development" (
echo Stopping DiscordDevelopment process...
taskkill /f /im discorddevelopment.exe >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to stop DiscordDevelopment process.
)
) else (
echo Invalid input. Exiting script.
goto :eof
)
echo Installing pnpm...
call npm i -g pnpm >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to install pnpm.
)
echo Changing to %userprofile%\replugged directory...
PUSHD %userprofile%\replugged >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to change to %userprofile%\replugged directory.
)
echo Updating global git configuration...
call git config --global --add safe.directory %userprofile%\replugged >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to update global git configuration.
)
echo Pulling latest changes from Git repository...
call git pull >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to pull latest changes from Git repository.
)
echo Unplugging %discordversion%...
call pnpm run unplug %discordversion% >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to unplug %discordversion%.
)
echo Installing dependencies...
call pnpm i >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to install dependencies.
)
echo Building project...
call pnpm run build >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to build project.
)
echo Plugging %discordversion%...
call pnpm run plug %discordversion% >nul 2>&1
if not %errorlevel% == 0 (
echo Failed to plug %discordversion%.
)
echo Launching %discordversion% update process...
if /i "%discordversion%" == "stable" (
START "" "%localappdata%\Discord\Update.exe" --processStart discord.exe
) else if /i "%discordversion%" == "ptb" (
START "" "%localappdata%\DiscordPTB\Update.exe" --processStart discordptb.exe
) else if /i "%discordversion%" == "canary" (
START "" "%localappdata%\DiscordCanary\Update.exe" --processStart discordcanary.exe
) else if /i "%discordversion%" == "development" (
START "" "%localappdata%\DiscordDevelopment\Update.exe" --processStart discorddevelopment.exe
)
echo Restoring original current directory...
POPD
echo Done.
所有输入都会发生这种情况,因此我提供了整个过程
我尝试过将输入更改为同时接受“y“和“y”,但都不起作用。
1条答案
按热度按时间plicqrtu1#
几个问题:
如果
errorlevel
为0,则代码将goto
为标签,因此不需要else
子句。尝试使用
接下来,通常的做法是添加一个
命令。其效果是脚本对环境变量所做的任何更改在脚本结束时都会被撤消。如果没有该机制,您将需要手动重置已更改的变量,或初始化要使用的变量。如果您不重置/初始化变量,则它们会保留上次运行时的值,直到它们被专门更改。
接下来,如果只按Enter键,
SET /P "var=Prompt"
不会更改var
。因此,如果var
最初为空,它将保持为空。因此,建议您先对使用的变量执行set
操作(有时将其设置为默认值非常有用,这样用户只需按Enter键即可使用默认值)最后,代码使用
set /p
更改code block
(带括号的命令序列)中的变量值。尽管删除
else
子句会使此观察结果对于当前代码来说在很大程度上是多余的,但您需要了解delayed expansion trap在块语句中,整个块被解析,然后被执行。块中的任何
%var%
都将被替换为该变量的值在块被解析时-在块被执行之前--同样的事情也适用于FOR ... DO (block)
。因此,
IF (something) else (somethingelse)
将使用遇到IF
时%variables%
的值执行,而不是在(块)内更改。