Windows 일괄 작업이 프로그램(실행 가능)을 시작하면 사용자는 Ctrl-C를 눌러 프로그램을 중지할 수 있습니다.
이로 인해 Windows 일괄 작업에서 "일괄 작업 종료(Y/N)" 프롬프트가 표시되고 키 누름을 기다립니다.
이 프롬프트와 아무 키나 눌러야 하는 필요성을 어떻게 억제할 수 있습니까?
답변1
@KTaTk의 제안을 확장하면 조건부에서 반환 코드를 입력하는 한 "일괄 작업 종료(Y/N)" 프롬프트가 표시되지 않습니다.
내 해결 방법은 다음과 같습니다
setlocal enabledelayedexpansion
program.exe & set SAVEDRC=!ERRORLEVEL! & call;
echo Return Code was %SAVEDRC%
exit /b %SAVEDRC%
(구문 분석 시간 확장)은 프로그램에서 업데이트된 반환 코드를 반영하지 않기 !ERRORLEVEL!
때문에 동일한 복합 명령문에서 값을 가져오려면 (지연된 확장)을 사용해야 합니다 .%ERRORLEVEL%
그런 다음 call;
함수는 다음 줄이 실행되기 전에 오류 수준을 0으로 재설정합니다.
마지막으로 (선택적으로) 저장된 반환 코드로 종료합니다. -1073741510( STATUS_CONTROL_C_EXIT
)이고 상위 프로세스도 cmd 쉘인 경우 상위 프로세스에 해당 프롬프트가 표시됩니다.
또는 ctrl-c 프롬프트는 해당 종료 코드를 유발하는 다른 프로세스의 반환 코드인 경우에만 발생하는 것 같습니다. 그래서 생성기능ERRORLEVEL을 변경하지 않고 종료하면 프롬프트도 표시되지 않습니다.
program.exe & call:ignoreCtrlC
echo Return Code was %ERRORLEVEL%
goto:eof
:ignoreCtrlC
exit /b
답변2
당신이 사용할 수있는조건부 실행실행 파일을 시작하는 명령에서:
my_executable.exe && exit 0 || exit 1
그러면 실행 파일의 종료 코드에 따라 메시지가 표시되지 않고 코드 0 또는 1로 일괄 작업이 종료됩니다.
이는 Ctrl-C와 Ctrl-Break 모두에서 작동합니다.
답변3
CTRL-break 또는 CTRL-C가 이미 키 누르기인 경우 CTRL-C 또는 Ctrl-break를 누르지 않고도 배치 스크립트를 중지하는 답을 찾을 수 있습니다.
이 대답은 그것이 당신의 목표인 경우에 대한 것입니다.
Batch에서는 레이블과 IF 문을 사용하여 스크립트 내에서 이동할 수 있습니다.
예를 들어:
@echo off
:: -- Lets print something to the screen.
echo This is a test before the loop starts
:: -- Lets set a counter variable to 0, just to be sure.
set counter=0
:: -- Set loop beginning point
:begin
:: -- Increase the counter by one but don't display the result to the screen
set /a counter=counter+1 >nul
:: -- now, write it to the screen our way.
echo Progress: %counter% of 10
:: -- pause the script for a second but without any displaying...
ping localhost -n 2 >nul
:: -- see if we reached 10, if so jump to the exit point of the script.
if %counter%==10 goto exit
:: -- if we reached this point, we have not jumped to the exit point.
:: -- so instead, lets jump back to the beginning point.
:: -- identation is used so the loop clearly stands out in the script
goto begin
:: -- set exit point for this script
:exit
:: -- lets print a message. We don't have to, but its nice, right?
echo End of the script has been reached.
c:\temp\counter.cmd를 썼다고 가정하면 다음과 같은 출력이 생성됩니다.
c:\temp>counter
This is a test before the loop starts
Progress: 1 of 10
Progress: 2 of 10
Progress: 3 of 10
Progress: 4 of 10
Progress: 5 of 10
Progress: 6 of 10
Progress: 7 of 10
Progress: 8 of 10
Progress: 9 of 10
Progress: 10 of 10
End of the script has been reached.