배치 파일의 함수 / 방법과 같은 것입니까?
Java, C # 등에서 알고있는 것과 같은 방법을 모방하는 것이 있습니까? 배치 파일에 5 줄의 명령이 있는데, 그 5 줄은 배치 파일 내부의 여러 위치에서 사용됩니다. goto를 사용할 수 없습니다. 그 5 줄에 의해 생성 된 오류 수준에 따라 다른 작업이 수행되기 때문입니다. 5lines.bat 배치 파일에 5 줄을 넣으려고했지만 원래 배치 파일 original.bat는 5lines.bat 만 호출하고 5lines.bat 호출 후 명령을 실행하지 않습니다.) : 이것이 내 original.bat가 보이는 방식입니다. 처럼:
5lines.bat
echo this gets never called, how to make sure this gets called?
5lines.bat에는 출구 나 이와 비슷한 것이 없습니다! 5lines.bat 이후의 줄이 호출되었는지 어떻게 확인할 수 있습니까?
재사용 가능한 함수를 별도의 배치 파일에 배치하면 확실히 함수를 시뮬레이션 할 수 있습니다.
문제 call
는 두 번째 배치 파일이 실행을 마친 후 제어가 호출자에게 반환되도록하기 위해 명령 을 사용해야한다는 것 입니다.
call 5lines.bat
echo this will now get called
call 명령을 사용할 수 있습니다.
call:myDosFunc
그런 다음 다음과 같이 함수를 정의하십시오.
:myDosFunc - here starts the function
echo. here the myDosFunc function is executing a group of commands
echo. it could do a lot of things
goto:eof
출처 : 배치 기능
완전성을 위해 매개 변수를 함수에 전달할 수도 있습니다.
함수 호출
call :myDosFunc 100 "string val"
기능 본체
:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
goto :eof
해결책:
@ECHO OFF
call:header Start Some Operation
... put your business logic here
... make sure EXIT below is present
... so you don't run into actual functions without the call
call:header Operation Finished Successfully
EXIT /B %ERRORLEVEL%
:: Functions
:header
ECHO =================================================
ECHO %*
ECHO =================================================
EXIT /B 0
EXIT / B를 각 함수의 끝과 함수 정의가 시작되기 전에 두는 것이 중요합니다.이 예에서는 다음과 같습니다.
종료 / B % ERRORLEVEL %
이 페이지에 나열된 예를 사용해 볼 수 있습니다.
또는 공통 라인을 기본 라인에서 호출하는 다른 배치 파일에 넣을 수 있습니다.
다음 은 배치 파일에서 "익명"기능 을 사용할 수있는 '해킹'입니다 .
@echo off
setlocal
set "anonymous=/?"
:: calling the anonymous function
call :%%anonymous%% a b c 3>&1 >nul
:: here the anonymous function is defined
if "%0" == ":%anonymous%" (
echo(
echo Anonymous call:
echo %%1=%1 %%2=%2 %%3=%3
exit /b 0
)>&3
::end of the anonymous function
The anonymous function block should be placed right after the call statement and must end with exit statement
the trick is that CALL
internally uses GOTO
and then returns to the line where the CALL
was executed. With the double expansion GOTO help message is triggered (with %%/?%%
argument) and then continues the script. But after it is finished it returns to the CALL
- that's why the if statement is needed.
For another great tutorial on writing reusable batch file code -- see Richie Lawrence's excellent library.
I'm not sure if it was obvious from other answers but just to be explicit I'm posting this answer. I found other answers helpful in writing below code.
echo what
rem the third param gives info to which label it should comeback to
call :myDosFunc 100 "string val" ComeBack
:ComeBack
echo what what
goto :eof
:myDosFunc
echo. Got Param#1 %~1
echo. Got Param#2 %~2
set returnto=%~3
goto :%returnto%
ReferenceURL : https://stackoverflow.com/questions/10149194/something-like-a-function-method-in-batch-files
'programing' 카테고리의 다른 글
Linux에서 "system"과 "exec"의 차이점은 무엇입니까? (0) | 2022.07.19 |
---|---|
VueJS 메서드를 타이프스크립트 농담으로 어떻게 부를 수 있을까요? (0) | 2022.07.19 |
Visual Studio / C # : Nuget 원격 서버에 연결할 수 없음 (0) | 2021.01.17 |
Dalvik 가상 머신 인스턴스가 각 애플리케이션에 대해 생성됩니까? (0) | 2021.01.17 |
스크립트를 종료하기 전에 백그라운드 프로세스가 완료되기를 기다리는 중 (0) | 2021.01.17 |