Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

KJH

std in, out, error 본문

DevOps

std in, out, error

모이스쳐라이징 2024. 11. 19. 09:01

Shell에서는 표준 입력(Standard Input, stdin), 표준 출력(Standard Output, stdout), 표준 에러(Standard Error, stderr)라는 세 가지 주요 스트림이 존재 함

 

  • stdin: 0번 파일 디스크립터
  • stdout: 1번 파일 디스크립터
  • stderr: 2번 파일 디스크립터

 

 

stdin

프로그램이 입력 데이터를 받을 때 사용하는 스트림. 일반적으로 사용자가 키보드를 통해 프로그램에 데이터를 입력할 때 발생

cat > test.txt
test1
test2
# close(ctrl+d)

 

 

stdout

프로그램 실행 결과를 기본적으로 화면에 출력하는 스트림. 대부분의 명령어는 실행 결과를 stdout을 통해 표시함

echo "test1"
echo "test2" > output.txt

 

 

stderr

프로그램 실행 중 발생한 오류 메시지를 출력하는 스트림. 오류 메시지는 stderr를 통해 기본적으로 화면에 출력함

cat test1.txt
  L  cat: test1.txt: No such file or directory

 

 

리눅스 리다이렉션

bash에서 /dev/null 2>&1 이라는 문법을 보는 경우가 있는데 표준 에러를 표준 출력으로 redirection 하라는 의미.

(/dev/null에 전송된 데이터는 버려짐) 

 

stderror인 파일 디스크립터 2번을 stdout으로 redirection하여 오류 출력만 무시

ls notfound 2> /dev/null

 

 

stderror, stdout 출력 둘다 무시

ls notfound > /dev/null 2>&1

 

 

출력을 분리하여 로그를 다른파일로 저장 가능

ls notfound 1> success.txt 2> error.txt