#batch #shell 스크립트에서 에러 스노우볼링(errors snowballing)을 막자

less than 1 minute read

#!/bin/sh
set -e

github/scripts-to-rule-them-all 스크립트를 보면 모두 이렇게 시작한다. 뭘까?

Exit immediately if a pipeline (see Pipelines), which may consist of a single simple command (see Simple Commands), a list (see Lists), or a compound command (see Compound Commands) returns a non-zero status.

4.3.1 The Set Builtin / Bash Reference Manual

바로 종료시켜준다. 좋네. 에러 스노우볼링(errors snowballing)을 막아준다. 이 용어 마음에 든다. LoL에서 많이 듣던 용어이기도 하고.

batch script에도 비슷한 게 있을까? 없다. 까비.

pushd %cd%

command1 || goto :finally
command2 || goto :finally

:finally
set err=%errorlevel%

popd

@echo off
if %err% neq 0 (
   echo.
   echo ▄████████████▄▐█▄▄▄▄█▌
   echo █████▌▄▌▄▐▐▌██▌▀▀██▀▀
   echo ███▄█▌▄▌▄▐▐▌▀██▄▄█▌
   echo ▄▄▄▄█████████████
   echo.
)
@echo on

exit /b %err%

command마다 다 처리해줘야 한다. short-circuit evaluation을 사용해서 조금 더 간편하게 할 수 있을 뿐이다. 밑에 if 문 안 적어도 되는 게 어디야. 여기서 만족해야 한다.

참고로 %errorlevel% == 0 이면 true다.

참고