배치 스크립트에서 매개변수를 사용하여 배치 파일 실행

MD Aminul Islam 2022년6월21일
배치 스크립트에서 매개변수를 사용하여 배치 파일 실행

때로는 필요한 매개변수를 사용하여 현재 Batch 스크립트에서 새 Batch 스크립트를 실행해야 합니다. 이 문서에서는 배치 파일의 여러 매개변수를 사용하여 다른 배치 파일을 실행하는 방법에 대해 설명합니다.

배치 스크립트에서 매개변수를 사용하여 배치 파일 실행

매개변수가 있는 배치 스크립트를 실행하려면 YourScript.bat Parameter_1 Parameter_2 Parameter_3이라는 일반 형식을 따라야 합니다. %1 %2 ... %n과 같은 참조 스크립트에서 숫자 순서로 매개변수를 얻을 수 있습니다.

이해를 돕기 위해 설명과 함께 예를 들어 보겠습니다.

아래는 참조 스크립트입니다. 스크립트 내부의 코드는 아래에서 공유됩니다.

@echo off
ECHO The first parameter is %1
ECHO The second parameter is %2
ECHO The third parameter is %3

위의 코드는 매개변수를 인쇄합니다. 위의 코드를 참조하는 코드는 다음과 같습니다.

TestScript.bat 300 250 800

참조 스크립트의 이름을 TestScript.bat로 변경했습니다. Enter 키를 누르면 아래와 같은 출력이 나옵니다.

The first parameter is 300
The second parameter is 250
The third parameter is 800

복잡한 예를 들어 보겠습니다. 이제 매개변수를 가져오고 추가 작업을 수행하고 결과를 표시하는 참조 스크립트를 생성합니다.

위 참조 스크립트 코드의 수정된 버전은 다음과 같습니다.

@echo off
ECHO The first parameter is %1
ECHO The second parameter is %2
ECHO The third parameter is %3

이전과 같은 방법으로 스크립트를 호출하면 아래와 같은 출력을 얻을 수 있습니다.

TestScript.bat 300 250 800

출력:

The first parameter is 300
The second parameter is 250
The third parameter is 800
The result is 1350
MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

관련 문장 - Batch File