Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 10개 있습니다.)
개발 환경 구성: 1. batch 파일에서 실행한 exe에서 batch 실행 문맥의 환경 변수 설정
; https://www.sysnet.pe.kr/2/0/238

개발 환경 구성: 89. 배치(batch) 파일에서 또 다른 배치 파일을 동기 방식으로 실행 및 반환값 얻기
; https://www.sysnet.pe.kr/2/0/958

개발 환경 구성: 103. DOS batch - 동기 방식으로 원격 서비스 제어
; https://www.sysnet.pe.kr/2/0/989

개발 환경 구성: 144. 윈도우에서도 유닉스처럼 명령행으로 원격 접속하는 방법
; https://www.sysnet.pe.kr/2/0/1245

개발 환경 구성: 166. DOS - ping 결과에서 평균 응답 시간값 추출하기
; https://www.sysnet.pe.kr/2/0/1340

개발 환경 구성: 215. DOS batch - 하나의 .bat 파일에서 다중 .bat 파일을 (비동기로) 실행하는 방법
; https://www.sysnet.pe.kr/2/0/1629

개발 환경 구성: 242. 배치 파일에서 Thread.Sleep 효과를 주는 방법
; https://www.sysnet.pe.kr/2/0/1768

개발 환경 구성: 328. Visual Studio(devenv.exe)를 배치 파일(.bat)을 통해 실행하는 방법
; https://www.sysnet.pe.kr/2/0/11293

스크립트: 13. 윈도우 배치(Batch) 스크립트에서 날짜/시간 문자열을 구하는 방법
; https://www.sysnet.pe.kr/2/0/11742

스크립트: 16. cmd.exe의 for 문에서는 ERRORLEVEL이 설정되지 않는 문제
; https://www.sysnet.pe.kr/2/0/12039




cmd.exe의 for 문에서는 ERRORLEVEL이 설정되지 않는 문제

예를 들어 다음과 같이 콘솔 프로그램을 만들고,

using System;

class Program
{
    static int Main(string[] args)
    {
        Console.WriteLine("test");
        return args.Length;
    }
}

이것을 for 문에서 사용하면,

REM test.bat

echo off

for /f %%i in ('ConsoleApp1.exe 1') do set CONRESULT=%%i
echo %ERRORLEVEL%

echo 결과는 원래 기댓값으로는 1이 나와야 하는데 0이 나옵니다. 어찌 보면 for 문의 복합적인 동작으로 인해 ERRORLEVEL이 전체적으로는 0이 되는 듯하지만 다음과 같은 테스트를 해보면,

ConsoleApp1.exe 1 2

for /f %%i in ('ConsoleApp1.exe 1') do set CONRESULT=%%i
echo %ERRORLEVEL%

이번엔 결과가 2가 나오는 것을 볼 수 있습니다. 왜냐하면 첫 번째 "ConsoleApp1.exe 1 2" 실행에서 2가 ERRORLEVEL로 반환되었고 이후 for 문은 ERRORLEVEL 값을 변경하지 않았기 때문입니다. (사실, for 문의 특성상 저렇게 동작하는 것이 올바르긴 합니다.) 저런 이유 때문에 콘솔 프로그램의 출력과 exit code를 함께 얻고 싶다면 for 문을 사용하면 안 되고 다음과 같은 식으로 임시 파일을 이용해야 합니다.

ConsoleApp1.exe > tmpFile.txt
echo %ERRORLEVEL%
set /p CONRESULT=<tmpFile.txt
del tmpFile.txt

echo "RESULT" %CONRESULT%

위와 같이 하면 ERRORLEVEL도 반영이 되고, 결과 값을 CONRESULT로 받아올 수 있지만 아쉽게도 이번에는 콘솔 프로그램의 출력 중 첫 번째 라인만 받아온다는 제약이 있습니다.

참고로, for 이외에도 ERRORLEVEL에 영향을 주지 않는 몇몇 명령어가 있습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 10/22/2019]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  [70]  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
11889정성태5/8/201911479개발 환경 구성: 438. mstest, QTAgent의 로그 파일 설정 방법
11888정성태5/8/201926614.NET Framework: 830. C# - 비동기 호출을 취소하는 CancellationToken의 간단한 예제 코드 [1]파일 다운로드1
11887정성태5/8/201913191.NET Framework: 829. C# - yield 문을 사용할 수 있는 메서드의 조건
11886정성태5/7/201912308오류 유형: 534. mstest.exe 실행 시 "Visual Studio Enterprise is required to execute the test." 오류 [2]
11885정성태5/7/20199275오류 유형: 533. mstest.exe 실행 시 "File extension specified '.loadtest' is not a valid test extension." 오류 발생
11884정성태5/5/201912956.NET Framework: 828. C# DLL에서 Win32 C/C++처럼 dllexport 함수를 제공하는 방법 - 두 번째 이야기
11883정성태5/3/201917203.NET Framework: 827. C# - 인터넷 시간 서버로부터 받은 시간을 윈도우에 적용하는 방법파일 다운로드1
11882정성태5/2/201914167.NET Framework: 826. (번역글) .NET Internals Cookbook Part 11 - Various C# riddles파일 다운로드1
11881정성태4/28/201914410오류 유형: 532. .NET Core 프로젝트로 마이그레이션 시 "CS0579 Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute" 오류 발생
11880정성태4/25/201911115오류 유형: 531. 이벤트 로그 오류 - Task Scheduling Error: m->NextScheduledSPRetry 1547, m->NextScheduledEvent 1547
11879정성태4/24/201916390.NET Framework: 825. (번역글) .NET Internals Cookbook Part 10 - Threads, Tasks, asynchronous code and others파일 다운로드2
11878정성태4/22/201914394.NET Framework: 824. (번역글) .NET Internals Cookbook Part 9 - Finalizers, queues, card tables and other GC stuff파일 다운로드1
11877정성태4/22/201914316.NET Framework: 823. (번역글) .NET Internals Cookbook Part 8 - C# gotchas파일 다운로드1
11876정성태4/21/201914151.NET Framework: 822. (번역글) .NET Internals Cookbook Part 7 - Word tearing, locking and others파일 다운로드1
11875정성태4/21/201914544오류 유형: 530. Visual Studo에서 .NET Core 프로젝트를 열 때 "One or more errors occurred." 오류 발생
11874정성태4/20/201914530.NET Framework: 821. (번역글) .NET Internals Cookbook Part 6 - Object internals파일 다운로드1
11873정성태4/19/201913480.NET Framework: 820. (번역글) .NET Internals Cookbook Part 5 - Methods, parameters, modifiers파일 다운로드1
11872정성태4/17/201913952.NET Framework: 819. (번역글) .NET Internals Cookbook Part 4 - Type members파일 다운로드1
11871정성태4/16/201913814.NET Framework: 818. (번역글) .NET Internals Cookbook Part 3 - Initialization tricks [3]파일 다운로드1
11870정성태4/16/201911537.NET Framework: 817. Process.Start로 실행한 콘솔 프로그램의 출력 결과를 얻는 방법파일 다운로드1
11869정성태4/15/201915308.NET Framework: 816. (번역글) .NET Internals Cookbook Part 2 - GC-related things [2]파일 다운로드2
11868정성태4/15/201913004.NET Framework: 815. CER(Constrained Execution Region)이란?파일 다운로드1
11867정성태4/15/201912009.NET Framework: 814. Critical Finalizer와 SafeHandle의 사용 의미파일 다운로드1
11866정성태4/9/201915586Windows: 159. 네트워크 공유 폴더(net use)에 대한 인증 정보는 언제까지 유효할까요?
11865정성태4/9/201911333오류 유형: 529. 제어판 - C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Administrative Tools is not accessible.
11864정성태4/9/201910528오류 유형: 528. '...' could be '0': this does not adhere to the specification for the function '...'
... 61  62  63  64  65  66  67  68  69  [70]  71  72  73  74  75  ...